There is a file extension pattern:
const pattern = '.js';
How correctly to set a template, to make regex work?
const reg = /(.*\pattern$)/;
Use the regex constructor. Link to docs.
For your snippet:
const pattern = '.js';
const reg = new RegExp(`(.*${pattern}$)`);
This has the caveat that you have to double escape your backslashes (the first one escapes it in the string, so that the second actually makes it into the regex). I'm assuming the one present in the example was a part of your attempt to put the pattern in there, but if not then it should be
new RegExp(`(.*\\${pattern}$)`)
Related
I have a regular expression like this and it works fine using https://regexr.com/
([a-zA-Z0-9-.~%!$&'()*+,;=#]+(/[a-zA-Z0-9-.~%!$&'()+,;=:#]+)/?|(/[a-zA-Z0-9-.~%!$&'()*+,;=:#]+)+/?)(?[a-zA-Z0-9-.~%!$&'()+,;=:#/?])?(#[a-zA-Z0-9-._~%!$&'()+,;=:#/?])?
I would like to use it with RegExp() like below(I just put the above string inside the raw string), but it does not work. Do I need to do any other treatment?
const pattern =String.raw`([a-zA-Z0-9\-._~%!$&'()*+,;=#]+(\/[a-zA-Z0-9\-._~%!$&'()*+,;=:#]+)*\/?|(\/[a-zA-Z0-9\-._~%!$&'()*+,;=:#]+)+\/?)(\?[a-zA-Z0-9\-._~%!$&'()*+,;=:#/?]*)?(\#[a-zA-Z0-9\-._~%!$&'()*+,;=:#/?]*)?`;
let re = new RegExp(pattern);
return re.test(somestring)
I also tried enclose the regex with / and / like below and it does not work either. It allows spaces but I don't really allow space.
const re = new RegExp(/([a-zA-Z0-9\-._~%!$&'()*+,;=#]+(\/[a-zA-Z0-9\-._~%!$&'()*+,;=:#]+)*\/?|(\/[a-zA-Z0-9\-._~%!$&'()*+,;=:#]+)+\/?)(\?[a-zA-Z0-9\-._~%!$&'()*+,;=:#/?]*)?(\#[a-zA-Z0-9\-._~%!$&'()*+,;=:#/?]*)?/);
Updates:
I guess my question should be how do I make sure it matches full text like what I can do test in the website above(attached screenshot below)
I think the root of this question is that regexr is matching on the full string rather than just a part. .test() will return true if part of the regex matches. if you want to only return true when matching the on the full string I would suggest using start ^ and end $ delimiters.
const pattern =String.raw`^([a-zA-Z0-9\-._~%!$&'()*+,;=#]+(\/[a-zA-Z0-9\-._~%!$&'()*+,;=:#]+)*\/?|(\/[a-zA-Z0-9\-._~%!$&'()*+,;=:#]+)+\/?)(\?[a-zA-Z0-9\-._~%!$&'()*+,;=:#/?]*)?(\#[a-zA-Z0-9\-._~%!$&'()*+,;=:#/?]*)?$`;
let re = new RegExp(pattern);
console.log(re.test('asdf```'))
Match the beginning ^ and end $ of a string to get an exact match, otherwise a substring will be accepted.
const re = new RegExp('^regex$')
On the sample string:
const reStr = `^([a-zA-Z0-9\-._~%!$&'()*+,;=#]+(\/[a-zA-Z0-9\-._~%!$&'()*+,;=:#]+)*\/?|(\/[a-zA-Z0-9\-._~%!$&'()*+,;=:#]+)+\/?)(\?[a-zA-Z0-9\-._~%!$&'()*+,;=:#/?]*)?(\#[a-zA-Z0-9\-._~%!$&'()*+,;=:#/?]*)?$`
I'm fetching a regular expression from an external API, and it comes back as a string. I want to use the regex for address validation, but I can't seem to properly escape the unwanted characters after calling new RegExp() on the string.
Here's the regex I want to use:
console.log(regexFromAPI);
Output
/((\W|^)box\s+(#\s*)?\d+|post\s+office|(\W|^)p\.?\s*o\.?\s+(#\s*)?\d+)/i
However, I can't use that -- I need it to actually be a regex first.
If I do, for example:
const pattern = new RegExp(regexFromAPI);
and then:
console.log(pattern);
I get the following:
Output
//((W|^)boxs+(#s*)?d+|posts+office|(W|^)p.?s*o.?s+(#s*)?d+)/i/
My question is... why is this happening, and how can I avoid it? I want to use my string literal as a regex.
Thanks in advance.
The RegExp constructor does not expect a string with / delimiters, nor with options past the final /. If you do that, the pattern generated from calling new RegExp with it will result in one that matches a string which starts with a literal forward slash /, and ends with a forward slash / followed by the flag characters (here, i).
Instead, you should pass the pattern string without / delimiters, and pass the flags as the second argument - you can extract these easily by using another regular expression:
const fullPatternStr = String.raw`/((\W|^)box\s+(#\s*)?\d+|post\s+office|(\W|^)p\.?\s*o\.?\s+(#\s*)?\d+)/i`;
const [, pattern, flags] = fullPatternStr.match(/\/(.*)\/([a-z]*)/);
const regex = new RegExp(pattern, flags);
console.log(regex);
Take off the slashes and flags, then reconstruct it:
const str = String.raw`/((\W|^)box\s+(#\s*)?\d+|post\s+office|(\W|^)p\.?\s*o\.?\s+(#\s*)?\d+)/i`;
let regexBody = str.slice(1, str.lastIndexOf("/"));
let flags = str.split("/")[str.split("/").length - 1];
let regex = new RegExp(regexBody, flags);
console.log(regex);
Need help getting the following JavaScript RegEx case insensitive:
^(ABCDE)\d{5}$
I have tried /i but it doesn't work:
^(ABCDE)\d{5}$/i
Where should I place /i to get it to work?
Thanks in advance.
When you have a literal regex notation, just use /.../:
var re = /^(ABCDE)\d{5}$/i;
If you use a RegExp constructor:
var re = RegExp("^(ABCDE)[0-9]{5}$", "i");
However, the literal notation is preferable here since the pattern is constant, known from the beginning, and no variables are used to build it dynamically. Note that if you were to use \d in the RegExp constructor, you'd have to double the backslashes:
var re = RegExp("^(ABCDE)\\d{5}$", "i");
Try to write it this way :
var regex = /^(ABCDE)\d{5}$/i;
It needs the first / or you can also use
var regex = new RegExp('^(ABCDE)\\d{5}$', 'i');
Then if you try in a console this it should work (online testers can add other issue, just try directly on your code) :
regex.test('ABCDE12345') // true
regex.test('abcde12345') // true
Test on Regex101 : https://regex101.com/r/zR5yR0/1
I have a small javascript funtcion :
function GetFilteredListLimited(event) {
var $source = $(event.target);
var $Pattern = event.data.Pattern;
var RE = new RegExp($Pattern, 'i');
if (RE.test($source.val())) {
console.log('RegEx match');
}
};
The pattern used is:
^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$
Which should match most email-addresses.
Using http://regexpal.com/ I can see that the Pattern is correct. But for some weird reason the script already matches at the 4th character after #
abc#abcd should not give a match, but is does.
Any suggestions ?
You need to be aware of RegExp constructor where escaped characters must be double-escaped. So, your regex string passed to the RegExp constructor should look like:
^[A-Z0-9._%+-]+#[A-Z0-9.-]+\\.[A-Z]{2,4}$
The fix can be introduced like this:
var RE = new RegExp($Pattern.replace(/\\/g, '\\\\'), 'i');
It will work if the escape symbols are used consistently.
I've seen plenty of regex examples that will not allow any special characters. I need one that requires at least one special character.
I'm looking at a C# regex
var regexItem = new Regex("^[a-zA-Z0-9 ]*$");
Can this be converted to use with javascript? Do I need to escape any of the characters?
Based an example I have built this so far:
var regex = "^[a-zA-Z0-9 ]*$";
//Must have one special character
if (regex.exec(resetPassword)) {
isValid = false;
$('#vsResetPassword').append('Password must contain at least 1 special character.');
}
Can someone please identify my error, or guide me down a more efficient path? The error I'm currently getting is that regex has no 'exec' method
Your problem is that "^[a-zA-Z0-9 ]*$" is a string, and you need a regex:
var regex = /^[a-zA-Z0-9 ]*$/; // one way
var regex = new RegExp("^[a-zA-Z0-9 ]*$"); // another way
[more information]
Other than that, your code looks fine.
In javascript, regexs are formatted like this:
/^[a-zA-Z0-9 ]*$/
Note that there are no quotation marks and instead you use forward slashes at the beginning and end.
In javascript, you can create a regular expression object two ways.
1) You can use the constructor method with the RegExp object (note the different spelling than what you were using):
var regexItem = new RegExp("^[a-zA-Z0-9 ]*$");
2) You can use the literal syntax built into the language:
var regexItem = /^[a-zA-Z0-9 ]*$/;
The advantage of the second is that you only have to escape a forward slash, you don't have to worry about quotes. The advantage of the first is that you can programmatically construct a string from various parts and then pass it to the RegExp constructor.
Further, the optional flags for the regular expression are passed like this in the two forms:
var regexItem = new RegExp("^[A-Z0-9 ]*$", "i");
var regexItem = /^[A-Z0-9 ]*$/i;
In javascript, it seems to be a more common convention to the user /regex/ method that is built into the parser unless you are dynamically constructing a string or the flags.