Add a variable into a regex [duplicate] - javascript

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 2 years ago.
I've got a little problem of comprehension with my regex
I just want add a variable inside my regex
I've got this:
static test(Prefix, version: string) {
return "/^" + Prefix + "[0-9]*.[0-9]*.[0-9]*-[0-9]*-(SNAPSHOT)$/.test(version);
}
// I don't understand where to put my last "
thanks for your help

You can build a regexp expression via new RegExp(string)

Related

Is there a feature in javascript such as deleting words starting with "<" and ending with ">"? [duplicate]

This question already has answers here:
Parse an HTML string with JS
(15 answers)
Cross-Browser Javascript XML Parsing
(3 answers)
Parse XML using JavaScript [duplicate]
(2 answers)
Trying to use the DOMParser with node js
(8 answers)
Closed 2 years ago.
Is there a feature in javascript such as deleting words starting with "<" and ending with ">"?
For example I want to extract html codes from this object
"description": "<mainText><stats><attention> 25</attention> Hareket Hızı</stats></mainText><br>",
Sounds like a job for regex.
I think this could do it:
// BAD CODE
function useRegex(input) {
let regex = /^<[a-zA-Z]+> <[a-zA-Z]+><[a-zA-Z]+>$/i;
return regex.test(input);
}
EDIT: seems like my idiot self has messed this one up. Don't use regex, as commented by someone else.
I personally hate regex, so here are some tools to help you
Website to help you get started
https://regexr.com/
And how to apply that in Javascript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Regex code creator. It's like magic
https://regex-generator.olafneumann.org/

Regex with variables in it [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 5 years ago.
/^([A-Za-z0-9]){1,8}$/
This is a normal way to write a regex in JavaScript but I want to construct the regex dynamically with a variable in between ().
Variable = [A-Za-z0-9]
This is how you can build a new regular expression from string:
var v = '[A-Za-z0-9]';
var regExp = new RegExp('^(' + v + '){1,8}$');
console.log(regExp);
Now you can use the regular expression regExp in your purpose

Replace values between parentheses using Javascript and Regex [duplicate]

This question already has answers here:
How can I replace a string in parentheses using a regex?
(4 answers)
Closed 7 years ago.
I need to replace the text between two parentheses using Regex in Javascript. For example:
var x = "I need to go (now)";
I need to replace 'now' with 'tomorrow'. I tried this, but it didn't work:
x.replace(/\(now)\b/g, 'tomorrow');
"I need to know (now)".replace(/\(now\)/g, 'tomorrow');
You don't need the \b and you need to escape the second ).

Remove Substring in Javascript [duplicate]

This question already has answers here:
How to remove text from a string?
(16 answers)
Closed 7 years ago.
In JavaScript, I need to find a substring between &J= and Key using regex and remove it from my url which could contain several substrings.
Here is my url:
SID=18608202881669&Act=432&Mode=1&CLk=T&Key58=6003&dotnetdll=TopCoConfigurator.dll&dotnetfunc=CasingSizeSummary&SID=18608202881669&F=&J=CasingSize/CasingSizeSummary.asp&Key58=6003&ccs_casingsizeid=6003
Thanks
You can learn Regular Expression here and code for your problem is:
Here you go:
SID="18608202881669&Act=432&Mode=1&CLk=T&Key58=6003&dotnetdll=TopCoConfigurator.dll&dotnetfunc=CasingSizeSummary&SID=18608202881669&F=&J=CasingSize/CasingSizeSummary.asp&Key58=6003&ccs_casingsizeid=6003";
var re = /&J=(.*?)&Key/;
SID = SID.replace(re, '');
alert(SID);
See Fiddle

variable inside pattern [duplicate]

This question already has answers here:
How do I perform a Javascript match with a pattern that depends on a variable?
(3 answers)
Closed 8 years ago.
var seperator = ',', group = 'red, blue';
//group.search(seperator/g) - g is not defined
group.search(/seperator/g) // looks for "seperator"
group.search('/' + seperator + '/g') // doesn't seem to find my "seperator"
And with that I'm out of ideas... How do I get my seperator within expression?
Thanks in advance!
You need to create new regexp Object
var test = new RegExp(seperator, 'g');
group.search(test)

Categories