Regex - syntax error javascript - javascript

After reading this Question I am trying to use this regex
/(!<[^>]+>)+/g
Like this
var regex = /(!<[^>]+>)+/g;
new RegExp('myString', regex)
but I get
Uncaught SyntaxError: Invalid flags supplied to RegExp constructor
'/(!<[^>]+>)+/g'
Anybody have idea how to use it right?

new Regexp is used when you have a regular expression in a string and you want to convert it to a regular expression object.
It is awful and you should almost never use it. (The exception is when you are dynamically generating a regex).
You don't need to use it: You created the object using a regular expression literal on the previous line.
To apply the regular expression to a string, use the match method:
var regex = /(!<[^>]+>)+/g;
console.log("myString".match(regex));
console.log("Trivial !<tag> example".match(regex));
console.log("!<tag attribute='>>>'> example".match(regex));
(But applying regex to HTML is usually a terrible idea).

RegExp is used to create regular expression from string
The second attribute should be flags like i , g, m but you're giving a regular expression itself.
The syntax of RegExp is RegExp(regexString,flags)
but you are using
var regex = /(!<[^>]+>)+/g;
new RegExp('myString', regex)
^^^^^^
You can't use the RegExp function to test a string.
if you want to check 'myString' against your regular expression, use regex.test('myString')
if you want to find all matches of the regex in 'myString', use 'myString'.match(regex)

Related

Get only one character case insensitive in a globally case sensitive RegExp in JavaScript [duplicate]

I am using Nodejs to build application in which I need to process certain strings I have used the JS "RegExp" object for this purpose.
I want only a part of my string in the regex to be case insensitive
var key = '(?i)c(?-i)ustomParam';
var find = '\{(\\b' + key +'\\b:?.*?)\}';
var regex = new RegExp(find,"g");
But it breaks with following error
SyntaxError: Invalid regular expression: /{(\b(?i)c(?-i)ustomParam\b:?.*?)}/
I will get the key from some external source like redis and the string to be matched from some other external source , I want that the first alphabet should be case-Insensitive and the rest of alphabets to be case-Sensitive.
When I get the key from external source I will append the (?i) before the first alphabet and (?-i) after the first alphabet.
I even tried this just for starters sake, but that also didn't work
var key ='customParam';
var find = '(?i)\{(\\b' + key +'\\b:?.*?)\}(?-i)';
var regex = new RegExp(find,"g");
I know I can use "i" flags instead of above ,but that's not my use case. I did it just to check.
JavaScript built-in RegExp does not support inline modifiers, like (?im), let alone inline modifier groups that can be placed anywhere inside the pattern (like (?i:....)).
Even XRegExp cannot offer this functionality, you can only use (?i) on the whole pattern declaring it at the pattern beginning.
In XRegExp, you can define the regex ONLY as
var regex = XRegExp('(?i)\\{(\\b' + key +'\\b:?.*?)\\}', 'g');
On May 27, 2020, still neither JavaScript native RegExp, nor XRegExp patterns support inline modifier groups (i.e. (?i:...)), nor placing them in any part of the pattern (as far as XRegExp is concerned).

Javascript syntax identification

Writing a regular expression, I found this code:
JSON.stringify(e.model.ProcedureDate).replace(/\"/g, '');
Why does /\"/g work without being quoted? It isn't a string, and from what I gather, javascript doesn't have literals... What would you call the text passed to .replace()?
It is a regular expression literal, equivalent to:
new RegExp('\\"', 'g')
in the same way as [] is equivalent to new Array(0).
Javascript has regular expression literals. That's an example of one.

How to make this youtube videoId parsing RegExp work in JS?

I'm trying to use this great RegEx presented here for grabbing a video id from any youtube type url:
parse youtube video id using preg_match
// getting our youtube url from an input field.
var yt_url = $('#yt_url').val();
var regexp = new RegExp('%(?:youtube(?:-nocookie)?\\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\\.be/)([^"&?/ ]{11})%','i');
var videoId = yt_url.match( regexp ) ;
console.log('vid: '+videoId);
My console is always giving me a null videoId though. Am I incorrectly escaping something in my regexp var? I added the a second backslash to escape the single backslashes already.
Scratching my head?
% are delimiters for the PHP you got the link from, Javascript does not expect delimiters when using new RegExp(). Also, it looks like \\. should probably be replaced with \. Try:
var regexp = new RegExp('(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})','i');
Also, you can create a regular expression literally by using Javascript's /.../ delimiters, but then you'll need to escape all of your /s:
var regexp = /(?:youtube(?:-nocookie)?\.com\/(?:[^/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\\.be\/)([^"&?\/ ]{11})/i;
Documentation
Update:
A quick update to address the comment on efficiency for literal expressions (/ab+c/) vs. constructors (new RegExp("ab+c")). The documentation says:
Regular expression literals provide compilation of the regular expression when the script is loaded. When the regular expression will remain constant, use this for better performance.
And:
Using the constructor function provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.
Since your expression will always be static, I would say creating it literally (the second example) would be slightly faster since it is compiled when loaded (however, don't confuse this into thinking it won't be creating a RegExp object). This small difference is confirmed with a quick benchmark test.

Matching regular expression string in Javascript

Does anyone know how to find regular expression string from javascript code?
e.g.
var pattern = /some regular expression/;
Is it possible to to with regular expression :) ?
If I got your question right, and you need a regular expression which would find all the regular expressions in a JavaScript program, then I don't think it is possible. A regular expression in JavaScript does not have to use the // syntax, it can be defined as a string. Even a full-blown JavaScript parser would not be smart enough to detect a regular expression here, for instance:
var re = "abcde";
var regexClass = function() { return RegExp; }
var regex = new regexClass()(re);
So I would give up this idea unless you want to cover only a few very basic cases.
You want a regex to match a regex? Crazy. This might cover the simplest cases.
new RegExp("\/.+\/")
However, I peeked into the Javascript Textmate bundle and is has 2 regex for finding a regex start and end.
begin = '(?<=[=(:]|^|return)\s*(/)(?![/*+{}?])'
end = '(/)[igm]*';
Which you could probably use as inspiration for toward your goal.
Thanks for answers I have found also that it is nearly impossible task to do, but here is my regex which parses source code just fine:
this.mainPattern = new RegExp(//single line comment
"(?://.*$)|"+
//multiline comment
"(/\\*.*?($|\\*/))"+
//single or double quote strings
"|(?:(?:\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")|(?:'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'))"+
//regular expression literal in javascript code
"|(?:(?:[/].+[/])[img]?[\\s]?(?=[;]|[,]|[)]))"+
//brackets
"|([{]|[(]|[\[])|([}]|[)]|[\\]])", 'g');

What is the difference between using "new RegExp" and using forward slash notation to create a regular expression?

Is there any difference between using new RegExp("regex"); and /same_regex/ to test against a target string? I am asking this question because I got different validating result while use these two approaches. Here is the snippet I used to validate an email field:
var email="didxga#gmail.comblah#foo.com";
var regex1 = new RegExp("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$");
var regex2 = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
//using RegExp object
if(regex1.test(email)) {
console.log("email matched regex1");
} else {
console.log("email mismatched regex1");
}
//using slash notation
if(regex2.test(email)) {
console.log("email matched regex2");
} else {
console.log("email mismatched regex2");
}
I got two inconsistent results:
email matched regex1
email mismatched regex2
I am wondering if there is any difference here or I omitted something in this specific example?
For an executable example please refer to here
If you use the constructor to create a new RegExp object instead of the literal syntax, you need to escape the \‍ properly:
new RegExp("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$")
This is necessary as in JavaScript any unknown escape sequence \x is interpreted as x. So in this case the \. is interpreted as ..
/.../ is called a regular expression literal. new RegExp uses the RegExp constructor function and creates a Regular Expression Object.
From Mozilla's developer pages
Regular expression literals provide
compilation of the regular expression
when the script is evaluated. When the
regular expression will remain
constant, use this for better
performance.
Using the constructor function
provides runtime compilation of the
regular expression. Use the
constructor function when you know the
regular expression pattern will be
changing, or you don't know the
pattern and are getting it from
another source, such as user input.
this will be a help for you
http://www.regular-expressions.info/javascript.html
see the 'How to Use The JavaScript RegExp Object' section
if you are using RegExp(regx) regx should be in string format ex:-
\w+ can be created as regx = /\w+/ or as regx = new RegExp("\\w+").
Difference is in escaping at least in your case.
When you use / / notation, you have to escape '/' with '\/', when you're using Regexp notation you escape quotes

Categories