open console and type
'\'.replace(/\\/g, '\');
fails because the slash in the string isn’t really in the string, it’s escaping ‘
'\\'.replace(/\\/g, '\');
works because it takes one slash and finds it.
your regex works.
More Related Contents:
- Javascript Array – Split string at numbers [closed]
- Why do regex constructors need to be double escaped?
- How can I match a whole word in JavaScript?
- How do I get the YouTube video ID from a URL?
- How can I match overlapping strings with regex?
- Javascript and regex: split string and keep the separator
- Javascript Regex: How to put a variable inside a regular expression? [duplicate]
- Javascript RegExp + Word boundaries + unicode characters
- How to escape regular expression special characters using javascript? [duplicate]
- How can I match multiple occurrences with a regex in JavaScript similar to PHP’s preg_match_all()?
- utf-8 word boundary regex in javascript
- Javascript – return string between square brackets
- JavaScript regular expression for Unicode emoji
- How to extract base URL from a string in JavaScript?
- Regular Expression For Arabic Language
- grunt (minimatch/glob) folder exclusion
- Split string into sentences in javascript
- JavaScript regex – How to wrap matches with tag?
- Split a string based on multiple delimiters
- How can I match on, but exclude a regex pattern?
- Javascript replace with reference to matched group?
- Regex to match words with hyphens and/or apostrophes
- Split by Caps in Javascript
- javascript get function body
- Javascript reg ex to match whole word only, bound only by whitespace
- How to remove invalid UTF-8 characters from a JavaScript string?
- Javascript regular expression for punctuation (international)?
- Can you retrieve multiple regex matches in JavaScript?
- How to properly escape characters in regexp
- How to loop all the elements that match the regex?