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
Post a Comment