How to create regular expression in a loop? [duplicate] - javascript

This question already has answers here:
Javascript Regex: How to put a variable inside a regular expression? [duplicate]
(9 answers)
Closed 4 years ago.
I currently have a regex that looks like:
const ignoreRegex = new RegExp(/^\/(?!fonts|static).*$/)
However, I also have a dynamic array of strings like "test" which need to be ignored too. I need to somehow map through this array and add each string to the regex, such that:
const ignoreRegex = new RegExp(/^\/(?!fonts|static + ignoreRoutes.map(x => `|${x}`) + ).*$/)
How do I do this?

You can omit the / / surrounding your regular expression and use a string in the RegExp constructor.
See the code below.
const ignoreFolders = ["fonts", "static"];
const ignoreRoutes = ["route1", "route2"];
const ignore = ignoreFolders.concat(ignoreRoutes);
const ignoreRegex = new RegExp(`^\/(?!${ignore.join("|")}).*$`);
console.log(ignoreRegex);
If you have any regex special characters in your string, they will be escaped automatically.

const ignoreRoutes = ["fonts","static","aaa","bbb","ccc"];
const ignoreRegex = new RegExp(`^\\/(?!${ignoreRoutes.join("|")}).*$`);

Related

How to validate composed strings with regex [duplicate]

This question already has answers here:
Check whether a string matches a regex in JS
(13 answers)
Closed 4 months ago.
I want to validate/write a regex of this form: uuid OR uuid-cust-uuid
It should ONLY return true when I test with a valid uuid OR a composed uuid like uuid-cust-uuid
Here is the regex I have written thus far:
const uuid =
/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}(\-cust\-[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12})?/;
You can use the test method and it will do the job.
const regexUUID = /[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}(\-cust\-[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12})?/;
const uuid = 'ec3bf3c6-85be-4169-971c-0c49be945b51';
console.log(regexUUID.test(uuid));
Edited:
As for your requirement, you can try something like this:
// Wrtie base uuid regex
const baseUUIDRegex = '[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}';
// Then use it inside another regex like this
const customRegex = `^${baseUUIDRegex}-cust-${baseUUIDRegex}$`;
console.log('ec3bf3c6-85be-4169-971c-0c49be945b51-cust-ec3bf3c6-85be-4169-971c-0c49be945b51'.match(customRegex));
Hope it will be helpful for you.

convert string regex to regex with tag i [duplicate]

This question already has answers here:
Changing the RegExp flags
(4 answers)
Closed 3 years ago.
ok, I have a problem here, in my database I store regexPath that made from URL.
I used this package path-to-regex
so I store my converted path to regex as String in the database like this:
let path = "/v1/manager/notification/all"
let regexPath = "/^\/v1\/manager\/notification\/all(?:\/)?$/i"
and I need test() function for checking some condition.
ofcourse test() function need a regex format for checking value is exists in regex.the only way I found in internet for convert string to regex is :
let RegexPattern = new RegExp(regexPath)
but RegExp function consider my i tag as a part of regex him self and it returns something like this:
/"\/^\\\/v1\\\/manager\\\/notification\\\/all\\\/page\\=([^\\=\\\/]+?)(?:\\\/)?$\/i"/
how should I solve this problem?
You need to extract the inner regex and flags before. Here's an example:
const path = "/v1/manager/notification/all"
const regexPath = "/^\/v1\/manager\/notification\/all(?:\/)?$/i"
const separator = regexPath.lastIndexOf('/')
const pattern = regexPath.slice(1, separator)
const flags = regexPath.slice(separator + 1)
const regex = new RegExp(pattern, flags)
console.log('Pattern: ' + pattern)
console.log('Flags: ' + flags)
console.log('Regex: ' + regex)
Per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp you need to extract the modifiers yourself and pass them in as the second parameter. You will also need to remove the leading and trailing slashes.
new RegExp(pattern[, flags])

Remove both words from string with JavaScript? [duplicate]

This question already has answers here:
Why does javascript replace only first instance when using replace? [duplicate]
(3 answers)
Closed 3 years ago.
I need to remove 2 words from a string. The words are _with and _and so raised_hand_with_fingers_and_splayed becomes raised_hand_fingers_splayed
The regex /_with|_and/ appears to work in https://regexr.com/ but when I use it with JavaScript only the _with is removed:
const str = `raised_hand_with_fingers_and_splayed`;
const newStr = str.replace(/_with|_and/,"")
You need the g modifier to perform multiple replacements. Otherwise it just replaces the first match.
const str = `raised_hand_with_fingers_and_splayed`;
const newStr = str.replace(/_with|_and/g,"")
console.log(newStr);

Replace/remove every backslash in a string [duplicate]

This question already has answers here:
Replace all backslashes in a string with a pipe
(2 answers)
Closed 4 years ago.
I am trying to remove every backslash of a string for an hour, but I cannot make it work.
Here is my string for instance:
[{\"file\":\"https:\\/n-adsadele.stjkwgjkw.co\/adwq
Here is what I tried:
const replaced = toString.replace(String.fromCharCode(92), String.fromCharCode(32));
const replaced = toString.replace("\\\\", "");
const replaced = toString.replace("\\", "");
const replaced = toString.replace(/\\/, "");
All of this does absolutely nothing.
You could use regex simply like :
var toString = '[{\"file\":\"https:\\/n-adsadele.stjkwgjkw.co\/adwq';
console.log(toString.replace(/\\/g, ""));

javascript Regular Expressions [duplicate]

This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 6 years ago.
I have the following url
http://www.test.info/?id=50&size=40
How do I get the value of the url parameter with regular expressions in javascript . i need the size value and also need the url without &?
only
http://www.test.info/?id=50
Thanks
Consider using split instead of a regex:
var splitted = 'http://www.test.info/?id=50&size=40'.split('&');
var urlWithoutAmpersand = splitted[0];
// now urlWithoutAmpersand => 'http://www.test.info/?id=50'
var sizeValue = splitted[1].split('=')[1] * 1;
// now sizeValue => 40
Just use this as your regex
size.*?(?=&|$)
here is some code you can use
var re = /size.*?(?=&|$)/g;
var myArray = url.match(re);
console.log(myArray);
you also can do it like this:
var re = new RegExp("size.*?(?=&|$)", "g");
Here is a regex pattern you could use.
^(.+)&size=(\d+)
The first group will be the url up to right before the '&' sign. The second group will be the value of the size parameter. This assumes id always comes before size, and that there are only two parameters: id and size.

Categories