variable case insensitive regex match javascript [duplicate] - javascript

This question already has an answer here:
Part of String Case Insensitive in JavaScript Regex (?i) option not working
(1 answer)
Closed 3 years ago.
I'd like to perform a case insensitive regex match for only some of the words in regex string. For example I have this search string:
var str = "one two three four five";
I'd like to inspect it at least one of the values in /One|TWO/ is present in string. For some values I am interested in case-sensitive match, for others in case-insensitive. So, for example:
str.match(/One|TWO/);
Search for One should be performed in case-insensitive manner, and search for TWO should be case-sensitive.
Is this variable case sensitive match possible with Javascript Regex engine? Appending i as in str.match(/One|TWO/i); causes a case-insensitive search for the whole regex string and not just parts of it.

Usually, in regex, there are those useful things:
(?i) : starts case-insensitive mode
(?-i) : turns off case-insensitive mode
So, you would be able to write (?i)One(?-i)TWO. That will make One case-insensitive and TWO case-sensitive.
However, they are not supported in JavaScript.
Now, since you said that you will know your regex search string at runtime; then, you have two options:
Create 2 regex search strings, one with \i and one without \i
var str = "one two three four five";
var insens_re = RegExp("One", "ig");
var sens_re = RegExp("TWO", "g");
console.log(insens_re);
console.log(sens_re);
var a = str.match(insens_re) || [];
var b = str.match(sens_re) || [];
console.log("Matches : ", a.concat(b));
Use the function written below in order to convert any case-insensitive word to a more complex regex search string
function makeInsensitive(s) {
var a = s.toUpperCase().split("");
var b = s.toLowerCase().split("");
return a.map((c, i) => "[" + b[i] + c + "]").join("");
}
var str = "one two three four five";
var re = RegExp(makeInsensitive("One") + "|TWO", "g");
console.log(re);
console.log("Matches : ", str.match(re));

Related

Same regex have different results in Java and JavaScript [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 3 years ago.
Same regex, different results;
Java
String regex = "Windows(?=95|98|NT|2000)";
String str = "Windows2000";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
System.out.println(m.matches()); // print false
JavaScript
var value = "Windows2000";
var reg = /Windows(?=95|98|NT|2000)/;
console.info(reg.test(value)); // print true
I can't understand why this is the case?
From the documentation for Java's Matcher#matches() method:
Attempts to match the entire region against the pattern.
The matcher API is trying to apply your pattern against the entire input. This fails, because the RHS portion is a zero width positive lookahead. So, it can match Windows, but the 2000 portion is not matched.
A better version of your Java code, to show that it isn't really "broken," would be this:
String regex = "Windows(?=95|98|NT|2000)";
String str = "Windows2000";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
while (m.find()) {
System.out.println(m.group()); // prints "Windows"
}
Now we see Windows being printed, which is the actual content which was matched.

Why are characters outside my capturing parentheses included in matches from my RegExp? [duplicate]

This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 6 years ago.
Working in JavaScript I have strings that might contain words prefixed by an "#". The string might look like this #one two #three. I want a RegExp that finds the words prefixed by the "#" but I don't want to include the "#" itself.
I created a RegExp looking like this /#(\w+)/g but when I match it against my strings the "#" is indeed included. Why is this? I assumed that if I wanted to include it then the "#" would be inside the capturing parentheses like this /(#\w+)/g
var s = '#one two #three'
var matches = s.match(/#(\w+)/g)
// matches is now [ '#one', '#three' ] but I want [ 'one', 'three' ]
Note with the result I currently get, there is of course no problem in getting the array I want, I can just remove the first character of each string. But what I want to know is:
Is it possible to change my RegExp to get the desired result?
And:
Why are characters outside my capturing parenthesis included in the result? I find it intuitive that only the characters inside the parenthesis should be included.
You need to access first capturing group
var re = /#(\w+)/gm;
var str = '#one two #three';
var m;
while ((m = re.exec(str)) !== null) {
print(m[1]);
}
Ideone Demo
JS Demo
var re = /#(\w+)/;
var arr = ['#one', 'two', '#three'];
arr.forEach(function(str) {
if (str.match(re))
document.writeln(str.match(re)[1] + '<br>');
});

Nothing to repeat with regexp in Javascript [duplicate]

This question already has an answer here:
Javascript Regular Expression not matching
(1 answer)
Closed 7 years ago.
I'm trying to replace all occurences of {0}, {1}, {2}, etc in a string with Javascript.
Example string:
var str = "Hello, my name is {0} and I'm {1} years.";
I'm tried the following to construct the regexp:
var regex1 = new RegExp("{" + i + "}", "g")
var regex2 = new RegExp("\{" + i + "\}", "g")
Both attempts throws the error:
Invalid regular expression: /{0}/: Nothing to repeat
I use replace like this:
str.replace(regex, "Inserted string");
Found all kinds of StackOverflow posts with different solutions, but not quite to solve my case.
The string literal "\{" results in the string "{". If you need a backslash in there, you need to escape it:
"\\{"
This will results in the regex \{..\}, which is the correct regex syntax.
Having said that, your approach is more than weird. Using a regex you should do something like this:
var substitues = ['foo', 'bar'];
str = str.replace(/\{(\d+)\}/, function (match, num) {
return substitutes[num];
});
In other words, don't dynamically construct a regex for each value; do one regex which matches all values and lets you substitute them as needed.

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

JavaScript Regular Expressions - g modifier doesn't work [duplicate]

This question already has answers here:
Why does a RegExp with global flag give wrong results?
(7 answers)
Closed 5 years ago.
I have the following code :
var str = "4 shnitzel,5 ducks";
var rgx = new RegExp("[0-9]+","g");
console.log( rgx.exec(str) );
The output on chrome and firefox is ["4"].
Why don't I get the result ["4","5"]?
exec does only search for the next match. You need to call it multiple times to get all matches:
If your regular expression uses the "g" flag, you can use the exec method multiple times to find successive matches in the same string.
You can do this to find all matches with exec:
var str = "4 shnitzel,5 ducks",
re = new RegExp("[0-9]+","g"),
match, matches = [];
while ((match = re.exec(str)) !== null) {
matches.push(match[0]);
}
Or you simply use the match method on the string `str:
var str = "4 shnitzel,5 ducks",
re = new RegExp("[0-9]+","g"),
matches = str.match(re);
By the way: Using the RegExp literal syntax /…/ is probably more convenient: /[0-9]+/g.
exec() always only returns one match. You get further matches you need to call exec repeatedly.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/exec

Categories