I was wondering how to use a regexp to match a phrase that comes after a certain match. Like:
var phrase = "yesthisismyphrase=thisiswhatIwantmatched";
var match = /phrase=.*/;
That will match from the phrase= to the end of the string, but is it possible to get everything after the phrase= without having to modify a string?
You use capture groups (denoted by parenthesis).
When you execute the regex via match or exec function, the return an array consisting of the substrings captured by capture groups. You can then access what got captured via that array. E.g.:
var phrase = "yesthisismyphrase=thisiswhatIwantmatched";
var myRegexp = /phrase=(.*)/;
var match = myRegexp.exec(phrase);
alert(match[1]);
or
var arr = phrase.match(/phrase=(.*)/);
if (arr != null) { // Did it match?
alert(arr[1]);
}
phrase.match(/phrase=(.*)/)[1]
returns
"thisiswhatIwantmatched"
The brackets specify a so-called capture group. Contents of capture groups get put into the resulting array, starting from 1 (0 is the whole match).
It is not so hard, Just assume your context is :
const context = "https://example.com/pa/GIx89GdmkABJEAAA+AAAA";
And we wanna have the pattern after pa/, so use this code:
const pattern = context.match(/pa\/(.*)/)[1];
The first item include pa/, but for the grouping second item is without pa/, you can use each what you want.
Let try this, I hope it work
var p = /\b([\w|\W]+)\1+(\=)([\w|\W]+)\1+\b/;
console.log(p.test('case1 or AA=AA ilkjoi'));
console.log(p.test('case2 or AA=AB'));
console.log(p.test('case3 or 12=14'));
If you want to get value after the regex excluding the test phrase, use this:
/(?:phrase=)(.*)/
the result will be
0: "phrase=thisiswhatIwantmatched" //full match
1: "thisiswhatIwantmatched" //matching group
Related
I have the following code:
var str = "$123";
var re = /(\$[0-9]+(\.[0-9]{2})?)/;
var found = str.match(re);
alert(found[1]);
alert(found[0]);
I am trying to understand why found[0] and found[1] would contain $123. Why does it get it twice?
I would like to get all the "potential" prices just one, so for example if I have this string:
var str = "$123 $149 $150"; It would be:
found[0] = $123
found[1] = $149
found[2] = $150
And that would be it, the array found would not have more matches.
What is happening here? What am I missing?
That's because of the parenthesis around the whole expression : it defines a captured group.
When you don't use the g flag, match returns in an array :
the whole string if it matches the pattern
the captured group(s)
Here the captured group is the whole string.
What you seem to want is
"$123 $149 $150".match(/\$\d+(\.\d{0,2})?/g)
which returns
["$123", "$149", "$150"]
Reference : the MDN about regular expressions and flags
The first is the full match.
The second represents the outer subgroup you defined, which is the same as the full match in your case.
That particular subgroup doesn't really seem necessary, so you should be able to remove it. The inner group doesn't have a match for your particular string.
FYI, if you want to use a group, but make it non-capturing, you can add ?: inside the start of it.
var re = /(?:\$[0-9]+(\.[0-9]{2})?)/;
Again, the group here isn't doing you much good, but it shows the ?: in use.
Add the g flag to the end of your regex. Otherwise only the first match will be captured. With g, sub groups are not captured. You do not need them to be; the outer parentheses in your regex do not actually do anything.
var re = /\$[0-9]+(\.[0-9]{2})?/g;
You can explicitly suppress subgroup capture with (?:, but it doesn't matter with the g flag.
I am trying to fetch the value after equal sign, its works but i am getting duplicated values , any idea whats wrong here?
// Regex for finding a word after "=" sign
var myregexpNew = /=(\S*)/g;
// Regex for finding a word before "=" sign
var mytype = /(\S*)=/g;
//Setting data from Grid Column
var strNew = "QCById=20";
var matchNew = myregexpNew.exec(strNew);
var newtype = mytype.exec(strNew);
alert(matchNew);
https://jsfiddle.net/6vjjv0hv/
exec returns an array, the first element is the global match, the following ones are the submatches, that's why you get ["=20", "20"] (using console.log here instead of alert would make it clearer what you get).
When looking for submatches and using exec, you're usually interested in the elements starting at index 1.
Regarding the whole parsing, it's obvious there are better solution, like using only one regex with two submatches, but it depends on the real goal.
You can try without using Regex like this:
var val = 'QCById=20';
var myString = val.substr(val.indexOf("=") + 1);
alert(myString);
Presently exec is returning you the matched value.
REGEXP.exec(SOMETHING) returns an array (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec).
The first item in the array is the full match and the rest matches the parenthesized substrings.
You do not get duplicated values, you just get an array of a matched value and the captured text #1.
See RegExp#exec() help:
If the match succeeds, the exec() method returns an array and updates properties of the regular expression object. The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured.
Just use the [1] index to get the captured text only.
var myregexpNew = /=(\S*)/g;
var strNew = "QCById=20";
var matchNew = myregexpNew.exec(strNew);
if (matchNew) {
console.log(matchNew[1]);
}
To get values on both sides of =, you can use /(\S*)=(\S*)/g regex:
var myregexpNew = /(\S*)=(\S*)/g;
var strNew = "QCById=20";
var matchNew = myregexpNew.exec(strNew);
if (matchNew) {
console.log(matchNew[1]);
console.log(matchNew[2]);
}
Also, you may want to add a check to see if the captured values are not undefined/empty since \S* may capture an empty string. OR use /(\S+)=(\S+)/g regex that requires at least one non-whitespace character to appear before and after the = sign.
I try to get the 00-8.
Why this code do not returns me the 00-8 ?
<script>
var pageDetailsSecond = "a='00-8'b='13-'a+='00-2'b+='3333'c='4'";
var phone1 = pageDetailsSecond.match("a='(.*)'");
var phone1 = phone1[0];
var card_Phone = phone1;
alert(card_Phone);
</script>
Actually I get a='00-8'.
Because what you try to match includes a=....
But when you find it, you can strip it from the match found.
Checked with jsfiddle: http://jsfiddle.net/pbo5x9dx/
var pageDetailsSecond = "a='00-8'b='13-'a+='00-2'b+='3333'c='4'";
alert(pageDetailsSecond)
var phones = pageDetailsSecond.match("a='(.*?)'");
var phone1 = phones[1];
alert(phone1)
** edit: ** fix for non-greedy match, checked with http://jsfiddle.net/pbo5x9dx/1/
Because the array returned by match() will contain the entire match in the first array slot, and the capture groups in subsequent elements.
The array contents will be:
[
[0] = "a='00-8'",
[1] = '00-8'
]
What you want is phone1[1] instead of phone1[0], which contains just the portion of the match specified by your capture group (.*).
Based on the updated question, the regex pattern should be changed to:
"a='(.*?)'"
By default, regex patterns try to match as much as possible (known as "greedy"). The pattern is saying "match any number of any characters between ' characters. This now includes 00-8'b='13-'a+='00-2'b+='3333'c='4. By adding the ?, this changes the behaviour to "lazy". In other words, match as little as possible, and your regex is back to matching only 00-8 as before.
This is an interesting one - I am looking for a JavaScript regex solution to extract different parts from a string. Any input is much appreciated.
Example -
";1XYZ123_UK;1;2.3;evt14=0.0|evt87=0.0,;1XYZ456_UK;4;5.6;evt14=0.0;fly;0;0;;;"
I am trying to extract just these bits from the string ignoring the rest-
“1XYZ123_UK;2;3;1XYZ456_UK;4;5.6;”
Basically extract anything starting with 1XYZ up until it encounters 'evt'.
var s = ';1XYZ123_UK;1;2e.3;evt14=0.0|evt87=0.0,;1XYZ456_UK;4;5.6;evt14=0.0';
var r = s.match(/1XYZ((?!evt).)*/g);
Will give you your desired strings:
["1XYZ123_UK;1;2e.3;", "1XYZ456_UK;4;5.6;"]
var s= ";1XYZ123_UK;1;2.3;evt14=0.0|evt87=0.0,;1XYZ456_UK;4;5.6;evt14=0.0"
s = s.replace(/(evt.+?(?:\||;|$))/g, "");
console.log(s) // ";1XYZ123_UK;1;2.3;1XYZ456_UK;4;5.6;"
Use groups ((...)) to capture parts of the matched string. After a successful match the substrings captured can be accessed via the array returned from String.match or Regex.exec.
The first element of the array (index 0) is the whole match, the next (index 1) is the first capture.
Eg.
var re = /1XY(.*)evt/
var result = theString.match(re)
then, if there is a match (result is not null) then
result[0]
will be the whole match (starting 1XY and ending evt) while
result[1]
will be the text between those strings.
i am new to regex. I am trying to parse all contents inside curly brackets in a string. I looked up this post as a reference and did exactly as one of the answers suggest, however the result is unexpected.
Here is what i did
var abc = "test/abcd{string1}test{string2}test" //any string
var regex = /{(.+?)}/
regex.exec(abc) // i got ["{string1}", "string1"]
//where i am expecting ["string1", "string2"]
i think i am missing something, what am i doing wrong?
update
i was able to get it with /g for a global search
var regex = /{(.*?)}/g
abc.match(regex) //gives ["{string1}", "{string2}"]
how can i get the string w/o brackets?
"test/abcd{string1}test{string2}test".match(/[^{}]+(?=\})/g)
produces
["string1", "string2"]
It assumes that every } has a corresponding { before it and {...} sections do not nest. It will also not capture the content of empty {} sections.
var abc = "test/abcd{string1}test{string2}test" //any string
var regex = /{(.+?)}/g
var matches;
while(matches = regex.exec(abc))
console.log(matches);
Try this:
var abc = "test/abcd{string1}test{string2}test" //any string
var regex = /{(.+?)}/g //g flag so the regex is global
abc.match(regex) //find every match
a good place to read about Regex in javascript is here, and a nice place to test is here
good luck!
Nothing wrong. But you'll need to look at your capturing groups (the second element in the array) to get the content you wanted (you can ignore the first). To get all occurences, it's not enough to run exec once, you'll need to loop over the results using match.
Edit: nevermind that, afaik you can't access capturing groups with match. A simpler solution would be using a positive lookahead, as Mike Samuel suggested.
This result:
["{string1}", "string1"]
is showing you that for the first match, the entire regex matched "{string1}" and the first capturing parentheses matched "string1".
If you want to get all matches and see all capturing parens of each match, you can use the "g" flag and loop through, calling exec() multiple times like this:
var abc = "test/abcd{string1}test{string2}test"; //any string
var regex = /{(.+?)}/g;
var match, results = [];
while (match = regex.exec(abc)) {
results.push(match[1]); // save first captured parens sub-match into results array
}
// results == ["string1", "string2"]
You can see it work here: http://jsfiddle.net/jfriend00/sapfm/
try this for file
const fs = require('fs');
fs.readFile('logs.txt', function(err, data) {
if(err) throw err;
const paragraph = "'" + data + "'";
const regex = /\d+\<;>\S+\<;>(\d+)\<;/g;
const found = paragraph.match(regex);
console.log(found);
})