Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
38
rated 0 times [  43] [ 5]  / answers: 1 / hits: 129594  / 15 Years ago, wed, may 20, 2009, 12:00:00

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?


More From » regex

 Answers
7

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


[#99491] Saturday, May 16, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
naomim

Total Points: 344
Total Questions: 95
Total Answers: 114

Location: Wales
Member since Mon, May 17, 2021
3 Years ago
naomim questions
;