1

I just want to test the URL for a question mark ? however I keep getting an invalid quantifier error message...

alert(window.location.href.search('?'));

I then tried things like...

alert(window.location.href.search('\?'));

/

alert(window.location.href.search(\?));

...without any luck.

3 Answers 3

4

.search converts the argument to a RegExp./?/ is an invalid RegExp. The escape does not work either, because a single backslash in the string is not converted to a RegExp-escaping backslash.

Use indexOf instead:

location.href.indexOf('?') !== -1; // If true, then found
Sign up to request clarification or add additional context in comments.

Comments

1

You need to supply a RegExp pattern for the "?" to be found by .search():

alert(window.location.href.search(/\?/));

2 Comments

Thanks, while client-side I'm less fanatical against using regex I'm going to use indexOf (been doing too much PHP of late). Still your code worked, thanks!
No problem. I may have as well post it just as a comment to your question, as it's not an answer anyway ;)
0

Actually doubly-escaped window.location.href.search('\\?') will work

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.