Question about RegExp

That makes no sense.

I got confused it has to do with using character classes like \s.

They’re not the same.

Just follow the advice i gave up above & you’ll be fine.

If you know exactly what you want to match, use literal /.../
If you dont know use RegExp().

what makes no sense?

Your suggestion is not correct.

They are not the same, but RegExp creates a literal notation. So essentially it will do the same thing, just different way of doing it.

@ieahleen has already explained it well. The RegExp takes 2 parameter. The first one is the Regex itself, and the second one is the flags for the regex, so like i, g, gi, etc…

Type this into your computer:

new RegExp('[gr\.]', 'ig')

then compare it to:

re = /[gr\.]/ig

Tell me what you get. Are they the same?

if you have something to say, don’t be cryptic about it, I have no idea what you are trying to say, I can’t read your mind

1 Like

Ummm. Yeah they are the same…

It’s the same thing, just different format of doing it. It also does the SAME EXACT thing.

Try again.

when you try to explain something in the simplest terms, you need to do some simplifications…

@Catalactics it’s about the fact that if you write new RegExp('[gr\.]', 'gi') it becomes /[gr.]/gi because to have a backslash in a string you need to escape it

So, you escaped a character. In RegExp you need to escape the character because it’s in string form, but you can totally escape it too. The only difference is that RegExp is in string and literal is… literal.

I know how it works…

My point is that it’s a mistake to claim:

let re = new RegExp(<a>, <b>); is /<a>/<b>

1 Like

Please remember the FCC Code of Conduct.

A simple reminder that standard string rules for escaping characters apply when using RegExp would have been helpful without being inflamatory. Thanks for your understanding.


Use literal for literals and RegExp for dynamic patterns.

1 Like

It’s not a mistake really, because that’s how to RegExp works. You just have to add more work to escape some character that needed to be escaped. But I see your point now.

if you know how simplifying work, then why do you claim it is a mistake?

If I make new RegExp("something", "flag") it becomes \something\flag not \somethingElse\flag

Your example is just about escaping characters in string, as console.log("[gr\.]") prints "[gr.]"

1 Like

Yes you used an example in which it works.

But you’re saying it’s the same and it’s not. Besides the point, as I’ve said before, RegExp should be used for dynamic patterns and regex literal should be used for literals.

1 Like

Your example is about strings in general, not regular expressions in particular.
But, I am adding a note to the post in my example, and bring up that general escaping rules for strings apply also in this case

I have never said something against this, but you still need to consider the general strings escaping rules for (for example) the string literals you write in array to build dinamically a series of regular expressions
the point you make is always valid, not only with the new RegExp thing.