Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript and regular expressions: literal syntax Vs. RegExp object

Tags:

I have some troubles with this small JavaScript code:

var text="Z Test Yeah ! Z";  // With literal syntax, it returns true: good! alert(/(Z[\s\S]*?Z)/g.test(text));  // But not with the RegExp object O_o var reg=new RegExp('Z[\s\S]*?Z','g'); alert(reg.test(text)); 

I don't understand why the literal syntax and the RegExp object don't give me the same result... The problem is that I have to use the RegExp object since I'll have some variables later.

Any ideas?

Thanks in advance :)

like image 386
KorHosik Avatar asked Aug 10 '11 17:08

KorHosik


People also ask

What is a RegExp object in JavaScript?

RegExp Object A regular expression is a pattern of characters. The pattern is used to do pattern-matching "search-and-replace" functions on text. In JavaScript, a RegExp Object is a pattern with Properties and Methods.

What is the syntax to define a regular expression in JavaScript?

RegExp Object A regular expression is a pattern of characters. The pattern is used to do pattern-matching "search-and-replace" functions on text. In JavaScript, a RegExp Object is a pattern with Properties and Methods.

What is difference [] and () in regex?

This answer is not useful. Show activity on this post. [] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.

What are object properties and object methods in regular expression?

A regular expression is an object that describes a pattern of characters. The JavaScript RegExp class represents regular expressions, and both String and RegExp define methods that use regular expressions to perform powerful pattern-matching and search-and-replace functions on text.

What is regexp object in JavaScript?

Regular Expressions and RegExp Object. A regular expression is an object that describes a pattern of characters. The JavaScript RegExp class represents regular expressions, and both String and RegExp define methods that use regular expressions to perform powerful pattern-matching and search-and-replace functions on text.

What is the use of regular expression literals?

Regular expression literals provide compilation of the regular expression when the script is loaded. If the regular expression remains constant, using this can improve performance. Or calling the constructor function of the RegExp object, as follows: let re = new RegExp('ab+c');

How do you construct a regular expression in JavaScript?

You construct a regular expression in one of two ways: 1 Using a regular expression literal, which consists of a pattern enclosed between slashes, as follows:#N#let re =... 2 Or calling the constructor function of the RegExp object, as follows:#N#let re = new RegExp('ab+c');#N#Using the constructor... More ...

How do I use regular expressions in Python?

Regular expressions are used with the RegExp methods test () and exec () and with the String methods match (), replace (), search (), and split (). Executes a search for a match in a string.


1 Answers

You need to double escape \ characters in string literals, which is why the regex literal is typically preferred.

Try:

'Z[\\s\\S]*?Z' 
like image 54
zzzzBov Avatar answered Sep 21 '22 15:09

zzzzBov