Sahil
Sahil

Reputation: 181

Unexptected token ^ in javascript using regex

I am using the following regex in javascript to do some replacement, but looks like JS doesn't like regex symbols; Is there a good way to escape them all. Following is my regex expression:

/(bower_components/[^.]+).css/g

Upvotes: 0

Views: 52

Answers (2)

user499054
user499054

Reputation:

The slash after bower_components is causing the Regex to end early.

Your regex is currently trying to form this object:

new RegExp('/(bower_components/', '[^.]+).css/g')

...which doesn't really make much sense.

You need to escape the slash like such:

/(bower_components\/[^.]+).css/g

Upvotes: 1

streetturtle
streetturtle

Reputation: 5850

If you want to escape them you need to put a backslash \ before each of them.

Upvotes: 1

Related Questions