0

I have the following String example :

test_client_guide..mydomain.fr.global.com=198787879898977979

I would like to extract in a variable the following String :

.mydomain.fr.global.com

In fact, i would like to get the String which is between the first "." and the "=" character.

How can i do that ?

Thanks a lot

2
  • Yes, you can do that. Commented Jan 23, 2014 at 14:27
  • You can use a regular expression, or you can use indexOf to find the positions of the characters and then use substr to extract the substring. Commented Jan 23, 2014 at 14:29

6 Answers 6

2

You could use regular expressions for that.

var string = 'test_client_guide..mydomain.fr.global.com=198787879898977979';
alert(string.replace(/^.*?\.(.+?)=.*$/, '$1'));

This would replace the following pattern:

  • ^ - start of the string
  • .*?\. - zero or more characters until the first .
  • (.+?)= - one or more characters until the first = (captured as $1)
  • .*$ - zero or more characters until the end of the string

with:

  • $1 - the capture above
Sign up to request clarification or add additional context in comments.

2 Comments

No need to match ^.* and .*$ - simply /\.(.+?)=/
@thg435, sure, but I'm using replace() to remove everything that's outside the first group.
1

Try this:

<script>
    var myString = "test_client_guide..mydomain.fr.global.com=198787879898977979";
    var str=myString.substring(myString.indexOf (".")+1,myString.indexOf("="));
    alert(str);
</script>

Comments

0

Use a regex, something like:

^.*?\.(.*)=

Would give you the piece you wanted in the first capture group.

See: http://rubular.com/r/WDIO27zriw

^: start of string

.*?: Match any character, non-greedy (the ? is key to making it not greedy)

\.: Match the first . - note it has to be escaped because . is a character class in regex (see the previous part)

(.*): Match any character (.) and put it in a capture group (the - ())

=: Match the =

2 Comments

You should make the .* lazy by making it .*?, so that it will take the first occurrence of that character, instead of the last occurrence. There'd be a difference in the result if you'd get to handle strings such as "foo..bar.baz=1337&quux=quok", since it'll match everything until the last equals sign.
@joeytje50: True, but the test string only has one = and it's not clear in the case of more than one = if the OP would want the first or the last (probably the first).
0

You can do this with substr and indexOf:

var exampleString = "test_client_guide..mydomain.fr.global.com=198787879898977979";
var firstdot = exampleString.indexOf('.')+1;
var firsteq = exampleString.indexOf('=', firstdot);
exampleString = exampleString.substring(firstdot, firsteq);
//exampleString now contains the contents ".mydomain.fr.global.com"

This gets your string from after the first period until the first equals sign after that.

Comments

0

this may be helpful

var s="test_client_guide..mydomain.fr.global.com=198787879898977979";
var _s=s.substring(s.indexOf("mydomain."),s.length);
_s=_s.substring(_s.indexOf("="),0)
console.log(_s)

DEMO

Comments

0

You can use a regular expression

 ^.*?(\..*)=

Edit to make numbered group since Javascript doesn't support named groups

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.