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
Related
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.
How I can get the value after last char(. ; + _ etc.):
e.g.
string.name+org.com
I want to get "com".
Is there any function in jQuery?
Use lastIndexOf and substr to find the character and get the part of the string after it:
var extension = name.substr(name.lastIndexOf(".") + 1);
Demo: http://jsfiddle.net/Guffa/K3BWn/
A simple and readable approch to get the substring after the last occurrence of a character from a defined set is to split the string with a regular expression containing a character class and then use pop() to get the last element of the resulting array:
The pop() method removes the last element from an array and returns that element.
See a JS demo below:
var s = 'string.name+org.com';
var result = s.split(/[.;+_]/).pop();
console.log(result);
to split at all non-overlapping occurrences of the regex by default.
NOTE: If you need to match ^, ], \ or -, you may escape them and use anywhere inside the character class (e.g. /[\^\-\]\\]/). It is possible to avoid escaping ^ (if you do not put it right after the opening [), - (if it is right after the opening [, right before the closing ], after a valid range, or between a shorthand character class and another symbol): /[-^\]\\]/.
Also, if you need to split with a single char, no regex is necessary:
// Get the substring after the last dot
var result = 'string.name+org.com'.split('.').pop();
console.log(result);
Not jQuery, just JavaScript: lastIndexOf and substring would do it (not since the update indicating multiple characters). As would a regular expression with a capture group containing a character class followed by an end-of-string anchor, e.g. /([^.;+_]+)$/ used with RegExp#exec or String#match.
E.g. (live copy | source):
var match = /([^.;+_]+)$/.exec(theStringToTest),
result = match && match[1];
var s = "string.name+org.com",
lw = s.replace(/^.+[\W]/, '');
console.log(lw) /* com */
this will also work for
string.name+org/com
string.name+org.info
You can use RegExp Object.
Try this code:
"http://stackoverflow.com".replace(/.*\./,"");
I'll throw in a crazy (i.e. no RegExp) one:
var s = 'string.name+org.com';
var a = s.split('.'); //puts all sub-Strings delimited by . into an Array
var result = a[a.length-1]; //gets the last element of that Array
alert(result);
EDIT: Since the update of the question is demanding mutiple delimiters to work this is probably not the way to go. Too crazy.....
use javascript function like
url.substr(url.length - 3);
maybe this is too late to consider, this codes works fine for me using jquery
var afterDot = value.substr(value.lastIndexOf('_') + 1);
You could just replate '_' to '.'
var myString = 'asd/f/df/xc/asd/test.jpg'
var parts = myString.split('/');
var answer = parts[parts.length - 1];
console.log(answer);
This is driving me crazy, what is the reason this doesn't work?
var name = data.match(/first-([A-Za-z0-9-]+)/g).replace('first-', '');
I want to replace first-joe with joe.
I also tried
var name = data.match(/first-([A-Za-z0-9-]+)/g);
var name = name.replace('first-', '');
and that doesn't work.
However the when alerting name I get first-joe
What is the reason for this, and how do I fix it?
Thanks
Try this instead:
var name = data.replace(/first-([A-Za-z0-9-]+)/g, '$1');
match with /g returns an array of matches (excluding parenthesized substrings), so you would have to replace them individually. If you know there is exactly one match, use data.match(/first-([A-Za-z0-9-]+)/)[1] which extracts the parenthesized substring.
I'm not sure why you're calling match first. Why not simply do either of these?
var name1 = data.replace('first-', '');
var name2 = data.replace(/^first-/, ''); //In case somebodys got a name containing first-.
Is it because your data variable contains something more than the string 'first-joe'?
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