How to capture group in Javascript Regex? - javascript

I am trying to capture customer.name from hello #customer.name from the end of the text string.
However, I can't seem to get the # character out of it. It gives #customer.name.
Right now my regex expression is:
#([0-9a-zA-Z.]+)$

Use the .match method of the string. The result will be null if there was no match with the given regex, otherwise it will be an array where:
The first element is the entire matched string, #customer.name in your case
The remaining elements are each capture group. You have one capture group so that would end up in the 1st index. In your case this will be customer.name, the string you want.
See this snippet. Your regex already looks correct, you just need to pull only the capture group from it instead of the entire matched string.
const str = "hello #customer.name"
const reg = /#([0-9a-zA-Z.]+)$/;
const match = str.match(reg)
console.log(match)
console.log("Matched string:", match[0]);
console.log("First capture group:", match[1]);

Your regex works fine, here's some code to use it to access your capture group using the regex .exec function. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec
let testString ='hello #customer.name',
pattern = /#([0-9a-zA-Z.]+)$/,
match = pattern.exec(testString);
console.log(match[1]); // abc

Related

Javascript word boundaries

I have seen this answer proposed in this question
However the resulting match is not the same. When the match is at the beginning of the string the string is returned, however when matched after a whitespace the whitespace is also returned as part of the match; even though the non-capture colon is used.
I tested with the following code is Firefox console:
let str1 = "un ejemplo";
let str2 = "ejemplo uno";
let reg = /(?:^|\s)un/gi;
console.log(str1.match(reg)); // ["un"]
console.log(str2.match(reg)); // [" un"]
Why is the whitespace being returned?
The colon in (?:^|\s) just means that it's a non-capturing group. In other words, when reading, back-referencing, or replacing with the captured group values, it will not be included. Without the colon, it would be reference-able as \1, but with the colon, there is no way to reference it. However, non-capturing groups are by default still included in the match. For instance My (?:dog|cat) is sick will still include the word dog or cat in the match, even though it's a non-capturing group.
To make it exclude the value, you have two options. If your regex engine supports negative look-behinds, you can use on of those, such as (?!<^|\s). If it does not (and unfortunately, JavaScript's engine is one of the ones which does not), you could put a capturing group around just the part you want and then read that group's value rather than the whole match (e.g, (?:^|\s)(un)). For instance:
let reg = /(?:^|\s)(un)/gi;
let match = reg.exec(input)
let result = match[1];
One solution would be to use a capturing group (ie. (un)) so that you can use RegExp.prototype.exec() and then use match[1] of this result to get the matched string, like this:
let str1 = "un ejemplo";
let str2 = "ejemplo uno";
let reg = /(?:^|\s)(un)/gi;
var match1 = reg.exec(str1);
var match2 = reg.exec(str2);
console.log(match1[1]); // ["un"]
console.log(match2[1]); // ["un"]

js regex - get string between expressions

i want to match string in javascript, but take only part from matched string.
E.g i have string 'my test string' and use match function to get only 'test':
var text = 'my test string';
var matched = text.match(/(?=my).+(?=string)/g);
iam tried to get it in this way but it will return 'my test'.
How can i do this to get only 'test' with regex?
You can use a capture group:
var match = text.match(/my (.*) string/g);
# match[0] will be the whole string, match[1] the capture group
match[1];
this will still match the whole string, but you can get the contents with match[1].
Some other regex engines have a feature called "lookbehind", but this is unsupported in JavaScript, so I recommend the method with the capture group.
You need to change your regex to /my (.+) string/g and create RegExp object from it:
var regex = new RegExp(/my (.+) string/g);
Then use regex.exec(string) to get the capturing groups:
var matches = regex.exec(text);
matches will be an array with the value: ["my test string", "test"].
matches contains 2 groups: $0 and $1. $0 is the whole match, and $1 is the first capturing group. $1 it's what inside the parentheses: .+.
You need $1, so you can get it by writing matches[1]:
//This will return the string you want
var matched = matches[1];

Substring specific text using javascript

<li class="item">hello?james</a></li>
<li class="item">goodbye?michael</a></li>
I want to extract the text that is after the sign "?" -> james, michael
I tried using a substring method but it only works if I specify the starting and the ending like substr(5,10) or substr(5) etc.
I'm using this when I extract from another file in a foreach php method, so I need everything that is after "?".
Is there any method in which I can substring starting with a character (eg. "?") or a specific string ?
Many thanks!
Use a regex with capture groups
Regular expressions will make it easy to match your HTML and using capture groups (parenthesis) you can extract the names from the string:
var myString = YOUR_HTML_HERE;
var myRegexp = /<li .*\?([a-zA-Z0-1]*)</g;
var match = myRegexp.exec(myString);
alert(match[1]); // james.
Execute the regex multiple times to get all matches
You can run the regex again in the original string to get the next match. To get all of them, do this:
while (match != null) {
match = myRegexp.exec(myString);
alert(match[1]); // michael
}

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.

Javascript regular expression matching prior and trailing characters

I have this string in a object:
<FLD>dsfgsdfgdsfg;NEW-7db5-32a8-c907-82cd82206788</FLD><FLD>dsfgsdfgsd;NEW-480e-e87c-75dc-d70cd731c664</FLD><FLD>dfsgsdfgdfsgfd;NEW-0aad-440a-629c-3e8f7eda4632</FLD>
this.model.get('value_long').match(/[<FLD>\w+;](NEW[-|\d|\w]+)[</FLD>]/g)
Returns:
[";NEW-7db5-32a8-c907-82cd82206788<", ";NEW-480e-e87c-75dc-d70cd731c664<", ";NEW-0aad-440a-629c-3e8f7eda4632<"]
What is wrong with my regular expression that it is picking up the preceding ; and trailing <
here is a link to the regex
http://regexr.com?30k3m
Updated:
this is what I would like returned:
["NEW-7db5-32a8-c907-82cd82206788", "NEW-480e-e87c-75dc-d70cd731c664", "NEW-0aad-440a-629c-3e8f7eda4632"]
here is a JSfiddle for it
http://jsfiddle.net/mwagner72/HHMLK/
Square brackets create a character class, which you do not want here, try changing your regex to the following:
<FLD>\w+;(NEW[-\d\w]+)</FLD>
Since it looks like you want to grab the capture group from each match, you can use the following code to construct an array with the capture group in it:
var regex = /<FLD>\w+;(NEW[\-\d\w]+)<\/FLD>/g;
var match = regex.exec(string);
var matches = [];
while (match !== null) {
matches.push(match[1]);
match = regex.exec(string);
}
[<FLD>\w+;] would match one of the characters inside of the square brackets, when I think what you actually want to do is match all of those. Also for the other character class, [-|\d|\w], you can remove the | because it is already implied in a character class, | should only be used for alternation inside of a group.
Here is an updated link with the new regex: http://jsfiddle.net/RTkzx/1

Categories

Resources