Javascript: Regex to escape parentheses and spaces -


looking backslash escape parentheses , spaces in javascript string.

i have string: (some string), , need \(some\ string\)

right now, i'm doing this:

x = '(some string)' x.replace('(','\\(') x.replace(')','\\)') x.replace(' ','\\ ') 

that works, it's ugly. there cleaner way go it?

you can this:

x.replace(/(?=[() ])/g, '\\'); 

(?=...) lookahead assertion , means 'followed by'

[() ] character class.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -