0

How can I get a javascript match text if I had some test example like :

someblablatext?someotherimportanttext

How can I capture everything after question mark sign (?). And alert it?

Was looking here didn't find some examples on this type of expressions? do I need to use group matching for this. Never used regex in javascript so far I did in Java many times.

3 Answers 3

1

You don't need regex for this. Simply use string.split() function, e.g.:

var yourString = "someblablatext?someotherimportanttext"
alert(yourString.split('?')[1]);

A while ago I heard that if you can use a string function instead of regex, then you probably should,. I did some benchmarking and it seems that string functions are very fast and in many cases faster than regexes (not talking about complex examples, just about something as simple as this).

Sign up to request clarification or add additional context in comments.

Comments

0

Use regexp.exec(string).

/\?(.*)$/.exec("someblablatext?someotherimportanttext")[1]

Comments

0

The above answers solve your issue but to be exact at your question (How can I capture everything AFTER question mark sign (?)), here is the regex you are looking for /(=?\?.*)/

=? means if followed by, \? mean question mark character, .* everything. To make them work you need to group them with (). That's it.

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.