113

This is a simple question I think.

I am trying to search for the occurrence of a string in another string using regex in JavaScript like so:

 var content ="Hi, I like your Apartment. Could we schedule a viewing? My phone number is: ";

 var gent = new RegExp("I like your Apartment. Could we schedule a viewing? My", "g");

 if(content.search(gent) != -1){   
     alert('worked');     
 }          

This doesn't work because of the ? character....I tried escaping it with \, but that doesn't work either. Is there another way to use ? literally instead of as a special character?

svlasov
  • 9,923
  • 2
  • 38
  • 39
Andrew
  • 3,650
  • 9
  • 31
  • 32
  • The worst part about this is that even using a string instead of a regex causes this problem, eg `str.search("?")` That definitely seems like a bug because that's not a regex and shouldn't be getting treated as a one. – Synetech Jun 18 '20 at 03:50

4 Answers4

182

You need to escape it with two backslashes

\\?

See this for more details:

http://www.trans4mind.com/personal_development/JavaScript/Regular%20Expressions%20Simple%20Usage.htm

Jonathan Holloway
  • 62,090
  • 32
  • 125
  • 150
  • 17
    … one for the regex and one for the string declration. – Gumbo May 20 '09 at 20:12
  • 2
    Mind blow! After years of working with regex, thought I saw everything.. But no, here you go. Double escape. Guess one day I'll see triple. – Geo Apr 17 '20 at 19:25
32

You should use double slash:

var regex = new RegExp("\\?", "g");

Why? because in JavaScript the \ is also used to escape characters in strings, so: "\?" becomes: "?"

And "\\?", becomes "\?"

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
17

You can delimit your regexp with slashes instead of quotes and then a single backslash to escape the question mark. Try this:

var gent = /I like your Apartment. Could we schedule a viewing\?/g;
jiggy
  • 3,828
  • 1
  • 25
  • 40
7

Whenever you have a known pattern (i.e. you do not use a variable to build a RegExp), use literal regex notation where you only need to use single backslashes to escape special regex metacharacters:

var re = /I like your Apartment\. Could we schedule a viewing\?/g;
                               ^^                            ^^

Whenever you need to build a RegExp dynamically, use RegExp constructor notation where you MUST double backslashes for them to denote a literal backslash:

var questionmark_block = "\\?"; // A literal ?
var initial_subpattern = "I like your Apartment\\. Could we schedule a viewing"; // Note the dot must also be escaped to match a literal dot
var re = new RegExp(initial_subpattern + questionmark_block, "g");

And if you use the String.raw string literal you may use \ as is (see an example of using a template string literal where you may put variables into the regex pattern):

const questionmark_block = String.raw`\?`; // A literal ?
const initial_subpattern = "I like your Apartment\\. Could we schedule a viewing";
const re = new RegExp(`${initial_subpattern}${questionmark_block}`, 'g'); // Building pattern from two variables
console.log(re); // => /I like your Apartment\. Could we schedule a viewing\?/g

A must-read: RegExp: Description at MDN.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563