Convert a string into a regular expression [duplicate] - javascript

This question already has answers here:
Converting user input string to regular expression
(14 answers)
Closed 9 years ago.
I am using a very fine JavaScript library called "array-query" by Jacob Wright to do searches in arrays of objects.
One method is regex() where a regular expression can be included in parentheses like this: regex(/[^\w\s]/). If I hardcode the expression as I just showed it works fine. If I put the same expression in a variable first it does not work, like this:
var reg = "/[^\w\s]/";
regex(reg);
I was told
You are putting quotes around your regex, making it a string. Remove the quotes.
Thus
var reg = /[^\w\s]/;
regex(reg);
works fine.
Problem is I need to accept the user input from an textbox as part of the regular expression. For example if the user types in the letter z it needs to get changed to /z/. Even if I type in /z/ the textbox.value returned has the same problem as a var reg = "/z/". If I hardcode var reg = /z/; regex(reg); it works fine.
How to make a input textbox value of "z" into a form that is var reg = z;?
Many many thanks for any help or ideas, hope this isn't too confusing.

You should do
var regex = new RegExp('your regex string');
Basically you can think of
var regex = /pattern/modifiers;
as
var regex = new RegExp(pattern,modifiers);
Read more about it at: MDN or w3schools

var reg = new RegExp("string");

You can do something like this:
var string = $('#input_id').val();
string = string.replace('/', '');
var regexpPattern = '/'+string+'/';
regex(regexpPattern);

Related

JavaScript RegExp: why `\$` don't match `$` but `\\$` do? [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 2 years ago.
I want to replace {$variable_name} in a string,like "Hi, my name is {$name}." will be change to "Hi, my name is Sam."
I thought $ will be represented as \$, many RegExp test websites, like, https://www.regextester.com/, do the same.
My code is followed.
var str = "Hi, my name is {$name}.";
var reg = "\{\$name\}";
str = str.replace(new RegExp(reg,"g"), "Sam");
console.log(str); //Hi, my name is {$name}.
Then, I change the \$ to \\$, it worked.
var str = "Hi, my name is {$name}.";
var reg = "\{\\$name\}";
str = str.replace(new RegExp(reg,"g"), "Sam");
console.log(str); //Hi, my name is Sam.
But I don't understand why, maybe my poor search skills, I didn't find an explanation.
Can anyone explain this to me? Thank you very much.
The reason for this is the way you are writing the regular expression. In a string, the \ character is used to escape, that is what is causing the behaviour you are seeing here. There is another way to write regular expressions which follows the same rules as these regex testing websites use. The difference is just wrapping the string in forward slashes instead of quotes.
var reg = /\{\$name\}/;
is the same as
var reg = "\{\\$name\}";

Pattern working on regex101 but not with Google Script [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I'm trying to match some paragraphs in Google Docs but the pattern that I wanted to use for it doesn't match the string when run inside a Google Script. However, it works properly on regex101 so I guess I'm missing something. Do you know what?
This is a sample of what I have:
function test() {
var str = "brown fox → jumps over the lazy dog";
var definitionRe = new RegExp('([\w\s]+)\s+[\u2192]\s+(.+)', 'g');
var definitionMatch = definitionRe.exec(str); // null
var dummy = "asdf"; // makes the debugger happy to break here
}
When using a string regex such as new RegExp(...), you need to escape your \'s, so then the following:
var definitionRe = new RegExp('([\w\s]+)\s+[\u2192]\s+(.+)', 'g');
Will become an escaped version like this:
var definitionRe = new RegExp('([\\w\\s]+)\\s+[\\u2192]\\s+(.+)', 'g');
Otherwise you can do a non string version, but you then can no longer concatenate values to the string (If that is something you would like):
var definitionRe = /([\w\s]+)\s+[\u2192]\s+(.+)/g;

Looking to trim a string using javascript / regex [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
I'm looking for some assistance with JavaScript/Regex when trying to format a string of text.
I have the following IDs:
00A1234/A12
0A1234/A12
A1234/A12
000A1234/A12
I'm looking for a way that I can trim all of these down to 1234/A12. In essence, it should find the first letter from the left, and remove it and any preceding numbers so the final format should be 0000/A00 or 0000/AA00.
Is there an efficient way this can be acheived by Javascript? I'm looking at Regex at the moment.
Instead of focussing on what you want to strip, look at what you want to get:
/\d{4}\/[A-Z]{1,2}\d{2}/
var str = 'fdfhfjkqhfjAZEA0123/A45GHJqffhdlh';
match = str.match(/\d{4}\/[A-Z]{1,2}\d{2}/);
if (match) console.log(match[0]);
You could seach for leading digits and a following letter.
var data = ['00A1234/A12', '0A1234/A12', 'A1234/A12', '000A1234/A12'],
regex = /^\d*[a-z]/gi;
data.forEach(s => console.log(s.replace(regex, '')));
Or you could use String#slice for the last 8 characters.
var data = ['00A1234/A12', '0A1234/A12', 'A1234/A12', '000A1234/A12'];
data.forEach(s => console.log(s.slice(-8)));
You could use this function. Using regex find the first letter, then make a substring starting after that index.
function getCode(s){
var firstChar = s.match('[a-zA-Z]');
return s.substr(s.indexOf(firstChar)+1)
}
getCode("00A1234/A12");
getCode("0A1234/A12");
getCode("A1234/A12");
getCode("000A1234/A12");
A regex such as this will capture all of your examples, with a numbered capture group for the bit you're interested in
[0-9]*[A-Z]([0-9]{4}/[A-Z]{1,2}[0-9]{2})
var input = ["00A1234/A12","0A1234/A12","A1234/A12","000A1234/A12"];
var re = new RegExp("[0-9]*[A-Z]([0-9]{4}/[A-Z]{1,2}[0-9]{2})");
input.forEach(function(x){
console.log(re.exec(x)[1])
});

Pattern check returning false javascript/typescript [duplicate]

This question already has answers here:
JavaScript RegExp objects
(5 answers)
Closed 6 years ago.
I am trying to validate if a string I entered matches the date format 'MM/yyyy'
Below is a sample of the code I am using for the same:
var date='05/2016'
var patt= new RegExp('^((0[1-9])|(1[0-2])|[1-9])\/(\d{4})$');
patt.test(date);
However the above code is returning false.
I tried running it with the regex checker:
https://regex101.com/
The pattern seems to be working fine.
Could someone please let me know what is missing.
https://jsfiddle.net/ymj6o8La/
You have to escape the string that is passed to RegExp (the backslashes).
var patt= new RegExp('^((0[1-9])|(1[0-2])|[1-9])\\/(\\d{4})$');
Even better, in your case, it's not dynamic, so you should use the literal RegExp instead
var patt = /^((0[1-9])|(1[0-2])|[1-9])\/(\d{4})$/
You should escape your backslashes. To represent \d or even \ you should another backslash behind it (e.g: \\) :
var date = '05/2016'
var patt = new RegExp('^((0[1-9])|(1[0-2])|[1-9])\\/(\\d{4})$');
console.log(patt.test(date));
Try using a pattern like this
patt= /^((0[1-9])|(1[0-2]))\/(\d{4})$/;

Extract specific data from JavaScript .getAttribute() [duplicate]

This question already has answers here:
Parse query string in JavaScript [duplicate]
(11 answers)
Closed 8 years ago.
So let's say I have this HTML link.
<a id="avId" href="http://www.whatever.com/user=74853380">Link</a>
And I have this JavaScript
av = document.getElementById('avId').getAttribute('href')
Which returns:
"http://www.whatever.com/user=74853380"
How do I extract 74853380 specifically from the resulting string?
There are a couple ways you could do this.
1.) Using substr and indexOf to extract it
var str = "www.something.com/user=123123123";
str.substr(str.indexOf('=') + 1, str.length);
2.) Using regex
var str = var str = "www.something.com/user=123123123";
// You can make this more specific for your query string, hence the '=' and group
str.match(/=(\d+)/)[1];
You could also split on the = character and take the second value in the resulting array. Your best bet is probably regex since it is much more robust. Splitting on a character or using substr and indexOf is likely to fail if your query string becomes more complex. Regex can also capture multiple groups if you need it to.
You can use regular expression:
var exp = /\d+/;
var str = "http://www.whatever.com/user=74853380";
console.log(str.match(exp));
Explanation:
/\d+/ - means "one or more digits"
Another case when you need find more than one number
"http://www.whatever.com/user=74853380/question/123123123"
You can use g flag.
var exp = /\d+/g;
var str = "http://www.whatever.com/user=74853380/question/123123123";
console.log(str.match(exp));
You can play with regular expressions
Well, you could split() it for a one liner answer.
var x = parseInt(av.split("=")[1],10); //convert to int if needed

Categories