Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
189
rated 0 times [  194] [ 5]  / answers: 1 / hits: 16717  / 9 Years ago, thu, september 3, 2015, 12:00:00

Hi all I tried to create some regex with random value.



var data = demo purpose?;  **OR**  var data = demo purpose;
var sentence = can I put these app as demo purpose?;
var re = new RegExp(\b( + data + )\b, g);
console.log(sentence.match(re)); // output [demo purpose]


In variable data have two different value demo purpose? & demo purpose with only question mark. Both console out are same please any one Give me hint what should i do in these case.



-
Thank you


More From » regex

 Answers
1

you need to escape ? (i.e. write \?) or else it would be interpreted as a quantifier in regex.



Furthermore, the \b is not really necessary because it tries to match a non blank char in which case there is nothing behind demo purpose? so sentence.match(new RegExp(\b(demo purpose\?)\b, g)) would return null.



If you want randomness, use Math.random. Make an array and get an random integer or 0 or 1 (with Math.floor) as the index.


[#65204] Tuesday, September 1, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dusty

Total Points: 739
Total Questions: 97
Total Answers: 85

Location: Angola
Member since Wed, Apr 13, 2022
2 Years ago
;