Chin
Chin

Reputation: 20705

Javascript regex handle /

I need to replace this string literally: /:)

However, if I do it like this

test = text.replace(//:\)/gi, replacement);

Javascript will treat // as the beginning of a comment. If I do it like this (add brackets):

test = text.replace(/(/:\))/gi, replacement);

this is a syntax error, since it will treat /(/ as the pattern

What can I do get around this?

Upvotes: 0

Views: 140

Answers (1)

nickb
nickb

Reputation: 59709

How about escaping the forward slash with a back slash also:

test = text.replace(/\/:\)/gi, replacement);

Upvotes: 3

Related Questions