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 :)
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.
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.
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.
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.
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.
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');
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 ...
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.
You need to double escape \
characters in string literals, which is why the regex literal is typically preferred.
Try:
'Z[\\s\\S]*?Z'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With