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.
Related
I have a Javascript array of string that contains urls like:
http://www.example.com.tr/?first=DSPN47ZTE1BGMR&second=NECEFT8RYD
http://www.example.com.tr/?first=RTR22414242144&second=YUUSADASFF
http://www.example.com.tr/?first=KOSDFASEWQESAS&second=VERERQWWFA
http://www.example.com.tr/?first=POLUJYUSD41234&second=13F241DASD
http://www.example.com.tr/?first=54SADFD14242RD&second=TYY42412DD
I want to extract "first" query parameter values from these url.
I mean i need values DSPN47ZTE1BGMR, RTR22414242144, KOSDFASEWQESAS, POLUJYUSD41234, 54SADFD14242RD
Because i am not good using regex, i couldnt find a way to extract these values from the array. Any help will be appreciated
Instead of using regex, why not just create a URL object out of the string and extract the parameters natively?
let url = new URL("http://www.example.com.tr/?first=54SADFD14242RD&second=TYY42412DD");
console.log(url.searchParams.get("first")); // -> "54SADFD14242RD"
If you don't know the name of the first parameter, you can still manually search the query string using the URL constructor.
let url = new URL("http://www.example.com.tr/?first=54SADFD14242RD&second=TYY42412DD");
console.log(url.search.match(/\?([^&$]+)/)[1]); // -> "54SADFD14242RD"
The index of the search represents the parameter's position (with index zero being the whole matched string). Note that .match returns null for no matches, so the code above would throw an error if there's no parameters in the URL.
Does it have to use regex? Would something like the following work:
var x = 'http://www.example.com.tr/?first=DSPN47ZTE1BGMR&second=NECEFT8RYD';
x.split('?first=')[1].split('&second')[0];
Try this regex:
first=([^&]*)
Capture the contents of Group 1
Click for Demo
Code
Explanation:
first= - matches first=
([^&]*) - matches 0+ occurences of any character that is not a & and stores it in Group 1
You can use
(?<=\?first=)[^&]+?
(?<=\?first=) - positive look behind to match ?first=
[^&]+? - Matches any character up to & (lazy mode)
Demo
Without positive look behind you do like this
let str = `http://www.example.com.tr/?first=DSPN47ZTE1BGMR&second=NECEFT8RYD
http://www.example.com.tr/?first=RTR22414242144&second=YUUSADASFF
http://www.example.com.tr/?first=KOSDFASEWQESAS&second=VERERQWWFA
http://www.example.com.tr/?first=POLUJYUSD41234&second=13F241DASD
http://www.example.com.tr/?first=54SADFD14242RD&second=TYY42412DD`
let op = str.match(/\?first=([^&]+)/g).map(e=> e.split('=')[1])
console.log(op)
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.
This has probably been asked a million times, and i am probably way off in what i have below, but i can not find it anywhere on SO. I need to get my alert below to show the value inside the brackets.
//the element(thisId) holds the following string: id[33]
var thisId = $(this).attr('id');
var idNum = new RegExp("\[(.*?)\]");
alert(idNum);
I need the alert to show the value 33.
You need to match the string with the regular expression, not just create the regexp. This returns an array containing the full match and the matches for capture groups.
var thisId = 'id[33]';
var match = thisId.match(/\[(.*?)\]/);
alert(match[1]); // Show first capture
You can use exec() to get the matches to your Regex:
var thisId = 'id[33]';
var matches = /\[(.*?)\]/g.exec(thisId);
alert(matches[1]); // you want the first group captured
Example fiddle
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