1

I have this expression to check wether if a key exists in a url :

new RegExp('/'+x+'/ig')

(removed the irrelevant code)

Thing is, it isn't working. I suppose I have to use delimiters (start and end) to work, since the url has many other things, but not sure how to do it.

Any thoughts?

2
  • Can you put up an example of the url and what you want the output to be? Commented Nov 18, 2010 at 21:38
  • Some examples of the URLs you're matching and the keys you're trying to match would make your question clearer. Commented Nov 18, 2010 at 21:39

2 Answers 2

4

When you use new Regex(), you have to pass the flags as a second parameter

new RegExp(x, 'ig')

Note that you don't need the literal delimiters when you create a RegExp this way

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

Comments

2

It kinda looks like you're mixing regexp literals and strings.

One way to write a regexp is like this:

var myExp = new RegExp('[a-z]*')

It matches any number of letters from a-z, any number of times. Another way to do the same thing would be like this:

var myExp = /[a-z]*/

or even

var myExp = /[a-z*]/ig

where ig means "ignore case" and "global".

So, stick to one way of writing it. Don't mix strings and literals.

Edit:

If you want to use the first syntax, but also ignore case and make the match global, pass the string 'ig' as a second argument to the RegExp-constructor (new RegExp('[a-z]*', 'ig'))

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.