regex match not outputting the adjacent matches javascript - javascript

i was experimenting on regex in javascript. Then i came across an issue such that let consider string str = "+d+a+", I was trying to output those characters in the string which are surrounded by +, I used str.match(/\+[a-z]\+/ig), so here what I'm expecting is ["+d+","+a+"], but what i got is just ["+d+"], "+a+" is not showing in the output. Why?

.match(/.../g) returns all non-overlapping matches. Your regex requires a + sign on each side. Given your target string:
+d+a+
^^^
^^^
Your matches would have to overlap in the middle in order to return "+a+".
You can use look-ahead and a manual loop to find overlapping matches:
var str = "+d+a+";
var re = /(?=(\+[a-z]\+))/g;
var matches = [], m;
while (m = re.exec(str)) {
matches.push(m[1]);
re.lastIndex++;
}
console.log(matches);

With regex, when a character gets consumed with a match, then it won't count for the next match.
For example, a regex like /aba/g wouldn't find 2 aba's in a string like "ababa".
Because the second "a" was already consumed.
However, that can be overcome by using a positive lookahead (?=...).
Because lookaheads just check what's behind without actually consuming it.
So a regex like /(ab)(?=(a))/g would return 2 capture groups with 'ab' and 'a' for each 'aba'.
But in this case it just needs to be followed by 1 fixed character '+'.
So it can be simplified, because you don't really need capture groups for this one.
Example snippet:
var str = "+a+b+c+";
var matches = str.match(/\+[a-z]+(?=\+)/g).map(function(m){return m + '+'});
console.log(matches);

Related

select the key and the value without the equal using regex

Hava a string like this:
"let key1=value1; let key2=value2;"
I want to select the key and value as groups using regex, I've tried using look around.
/(\w+)(?=\=)(\w+);/g
but it doesn't work with me, any suggestions?
The following regex should do the trick: (let (\w+) ?= ?(\w+);?)+.
Each let statement will be a match where the key will be the group 2 and the value the group 3.
The (?=\=) expression part is a lookahead, a zero-width assertion, it does not consume text but requires it to be present on the right. When you say (?=\=)(\w+) you want \w+ pattern to start matching on =. As \w does not match =, your regex always fails.
Use
/(\w+)=(\w+);/g
JavaScript (borrowed from How do you access the matched groups in a JavaScript regular expression?):
var myString = "let key1=value1; let key2=value2;";
var myRegexp = /(\w+)=(\w+);/g;
match = myRegexp.exec(myString);
while (match != null) {
console.log(match[1] + "," + match[2]);
match = myRegexp.exec(myString);
}
To be more specific, we could use (\w+) ?= ?(\w+);?+

Extract a specific word from string in Javascript

#anotherdata=value#iamlookingforthis=226885#id=101&start=1
Given the string above how could I extract "iamlookingforthis=226885" in the string? value of it might change as this is dynamic. So, other instance might be "iamlookingforthis=1105". The location/sequence might also change, it could be in the middle or last part.
Thank you in advance.
You can use Regex to match a specific text.
Like this for example
var str = '#anotherdata=value#iamlookingforthis=226885#id=101&start=1';
var value = str.match(/#iamlookingforthis=(\d+)/i)[1];
alert(value); // 226885
Explanation from Regex101:
#iamlookingforthis= matches the characters #iamlookingforthis= literally (case insensitive)
1st Capturing Group (\d+)
\d+ matches a digit (equal to [0-9])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
Global pattern flags
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
See
RegExp on MDN
Regex 101 - try regex and see the explanation of it and results
Another alternative would be to split the string. You could split it by #|?|&.
var str = '#anotherdata=value#iamlookingforthis=226885#id=101&start=1';
var parts = str.split(/[#\?&]/g); // split the string with these characters
// find the piece with the key `iamlookingforthis`
var filteredParts = parts.filter(function (part) {
return part.split('=')[0] === 'iamlookingforthis';
});
// from the filtered array, get the first [0]
// split the value and key, and grab the value [1]
var iamlookingforthis = filteredParts[0].split('=')[1];
alert(iamlookingforthis); // 226885
Here's a snippet:
var str = '#anotherdata=value#iamlookingforthis=226885#id=101&start=1';
var extracted = str.split("#").find(function(v){
return v.indexOf("iamlookingforthis") > -1;
});
alert(extracted); // iamlookingforthis=226885

Regular expression with asterisk quantifier

This documentation states this about the asterisk quantifier:
Matches the preceding character 0 or more times.
It works in something like this:
var regex = /<[A-Za-z][A-Za-z0-9]*>/;
var str = "<html>";
console.log(str.match(regex));
The result of the above is : <html>
But when tried on the following code to get all the "r"s in the string below, it only returns the first "r". Why is this?
var regex = /r*/;
var str = "rodriguez";
console.log(str.match(regex));
Why, in the first example does it cause "the preceding" character/token to be repeated "0 or more times" but not in the second example?
var regex = /r*/;
var str = "rodriguez";
The regex engine will first try to match r in rodriguez from left to right and since there is a match, it consumes this match.
The regex engine then tries to match another r, but the next character is o, so it stops there.
Without the global flag g (used as so var regex = /r*/g;), the regex engine will stop looking for more matches once the regex is satisfied.
Try using:
var regex = /a*/;
var str = "cabbage";
The match will be an empty string, despite having as in the string! This is because at first, the regex engine tries to find a in cabbage from left to right, but the first character is c. Since this doesn't match, the regex tries to match 0 times. The regex is thus satisfied and the matching ends here.
It might be worth pointing out that * alone is greedy, which means it will first try to match as many as possible (the 'or more' part from the description) before trying to match 0 times.
To get all r from rodriguez, you will need the global flag as mentioned earlier:
var regex = /r*/g;
var str = "rodriguez";
You'll get all the r, plus all the empty strings inside, since * also matches 'nothing'.
Use global switch to match 1 or more r anywhere in the string:
var regex = /r+/g;
In your other regex:
var regex = /<[A-Za-z][A-Za-z0-9]*>/;
You're matching literal < followed by a letter followed by 0 or more letter or digits and it will perfectly match <html>
But if you have input as <foo>:<bar>:<abc> then it will just match <foo> not other segments. To match all segments you need to use /<[A-Za-z][A-Za-z0-9]*>/g with global switch.

Javascript Regex match any word that starts with '#' in a string

I'm very new at regex. I'm trying to match any word that starts with '#' in a string that contains no newlines (content was already split at newlines).
Example (not working):
var string = "#iPhone should be able to compl#te and #delete items"
var matches = string.match(/(?=[\s*#])\w+/g)
// Want matches to contain [ 'iPhone', 'delete' ]
I am trying to match any instance of '#', and grab the thing right after it, so long as there is at least one letter, number, or symbol following it. A space or a newline should end the match. The '#' should either start the string or be preceded by spaces.
This PHP solution seems good, but it uses a look backwards type of functionality that I don't know if JS regex has:
regexp keep/match any word that starts with a certain character
var re = /(?:^|\W)#(\w+)(?!\w)/g, match, matches = [];
while (match = re.exec(s)) {
matches.push(match[1]);
}
Check this demo.
let s = "#hallo, this is a test #john #doe",
re = /(?:^|\W)#(\w+)(?!\w)/g,
match, matches = [];
while (match = re.exec(s)) {
matches.push(match[1]);
}
console.log(matches);
Try this:
var matches = string.match(/#\w+/g);
let string = "#iPhone should be able to compl#te and #delete items",
matches = string.match(/#\w+/g);
console.log(matches);
You actually need to match the hash too. Right now you're looking for word characters that follow a position that is immediately followed by one of several characters that aren't word characters. This fails, for obvious reasons. Try this instead:
string.match(/(?=[\s*#])[\s*#]\w+/g)
Of course, the lookahead is redundant now, so you might as well remove it:
string.match(/(^|\s)#(\w+)/g).map(function(v){return v.trim().substring(1);})
This returns the desired: [ 'iPhone', 'delete' ]
Here is a demonstration: http://jsfiddle.net/w3cCU/1/

Regex produces different result in javascript

Why does this regex return an entirely different result in javascript as compared to an on-line regex tester, found at http://www.gskinner.com/RegExr/
var patt = new RegExp(/\D([0-9]*)/g);
"/144444455".match(patt);
The return in the console is:
["/144444455"]
While it does return the correct group in the regexr tester.
All I'm trying to do is extract the first amount inside a piece of text. Regardless if that text starts with a "/" or has a bunch of other useless information.
The regex does exactly what you tell it to:
\D matches a non-digit (in this case /)
[0-9]* matches a string of digits (144444455)
You will need to access the content of the first capturing group:
var match = patt.exec(subject);
if (match != null) {
result = match[1];
}
Or simply drop the \D entirely - I'm not sure why you think you need it in the first place...
Then, you should probably remove the /g modifier if you only want to match the first number, not all numbers in your text. So,
result = subject.match(/\d+/);
should work just as well.

Categories

Resources