This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 8 years ago.
I got text like: do[A]and[B]
I want to extract all words that are wrapped by [ and ].
I am using the match method:
var text = "do[A]and[B]";
var regexp = /\[(\w+)\]/g;
var result = text.match(regexp);
So I am saying that I want to match all words wrapped by [ ], but only the wrapped part should be in group/memory. I keep getting the [ ] parentheses in result:
["[A]", "[B]"]
expected result is:
["A","B"]
I thought this is a piece of cake to do, but I must be missing something.
For this particular case you don't need capturing groups:
>>> "do[A]and[Bbbb]".match(/\w+(?=])/g);
["A", "Bbbb"]
will do.
In order to work with subpatterns, there is no easy shortcut.
Instead, you have to repeatedly execute the regex on the string and collect the subpattern you want. Something like this:
var text = "do[A]and[B]",
regexp = /\[(\w+)\]/g,
result = [], match;
while(match = regexp.exec(text)) {
result.push(match[1]);
}
Related
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 3 years ago.
I am a beginner when it comes to regex. I have string cn=foo,ou=bar,ou=zoo,ou=aa,ou=bb,ou=cc,ou=dd,o=someOrg,c=UK. I need to get foo,bar and zoo so I used following regex to extract string in javascript.
const dn = 'cn=foo,ou=bar,ou=zoo,ou=aa,ou=bb,ou=cc,ou=dd,o=someOrg,c=UK';
const regex = /^cn=(\w+),ou=(\w+),ou=(\w+)/;
const found = dn.match(regex);
console.log(found) --> Array ["cn=foo,ou=bar,ou=zoo", "foo", "bar", "zoo"]
Then ou=bar value is changed upon new requirement to ou=bar - 1. It could have - or numeric value in any order within that string value. I tried following regex.
const regex = /^cn=(\w+),ou=(.+),ou=(\w+)/;
however it returns unwanted data Array ["cn=foo,ou=bar,ou=zoo,ou=aa,ou=bb,ou=cc,ou=dd", "foo", "bar,ou=zoo,ou=aa,ou=bb,ou=cc", "dd"]
What I expect is Array ["cn=foo,ou=bar - 1,ou=zoo", "foo", "bar - 1", "zoo"]. I tried to exclude unwanted data via ^(ou=aa|ou=bb|ou=cc|ou=dd|o=someOrg|c=UK) within the regex but I got null value. I'd appreciate someone can help me to correct regex syntax.
Update:
I tried /^cn=(\w+),ou=(\w+\s+-\s+\d+),ou=(\w+)/ but this covers it above example but it won't cover something like ou=bar-1 or ou=1bar-..
The easies way to solve this task is to make it non-greedy (notice the question mark!):
const regex = /^cn=(\w+),ou=(.+?),ou=(\w+)/;
Alternatively, you may want to exclude the comma:
const regex = /^cn=(\w+),ou=([^,]+),ou=(\w+)/;
This question already has answers here:
javascript regex split produces too many items
(4 answers)
Closed 3 years ago.
Hi I have to split a string in two parts, the first one is always a char the second one is a number.
i.e.
a12
c4
I try to use this expression:
var myString = 'a12';
var mySplits = myString.split(/^([a-zA-Z]{1,2})([0-9]{1,2})$/);
console.log(mySplits);
the array expected is:
["a", "12"]
the result instead is:
["", "a", "12", ""]
I don't understand the reason why the result array has an empty value at start and at end.
Any help is really appreciated.
UPDATE
the solution proposed works but we can use a more elegant way.
ES18
With ES18 we could use the group capturing name
var myString = 'a12';
var matches =
/^(?<letter>[a-zA-Z]{1,2})(?<number>[0-9]{1,2})$/.exec(myString).concat();
console.log(matches.groups.letter);
console.log(matches.groups.number);
ES10
ES10 added the method .matchAll() that returns an Iterator, so if the need is not capture a single value but capture globally this method can be useful.
var myString = 'a12 b12 ';
for (const match of myString.matchAll(/(?<letter>[a-zA-Z]{1,2})(?<number>[0-9]{1,2})/g)) {
console.log(match.groups.letter);
console.log(match.groups.number);
}
var myString = 'a12';
var list = /^([a-zA-Z]{1,2})([0-9]{1,2})$/.exec(myString).concat();
list.shift(); // remove the first element
console.log(list); //This is the list that you want
This question already has answers here:
Regex: Match desired chars at the beginning/end of string
(1 answer)
What do ^ and $ mean in a regular expression?
(2 answers)
Closed 3 years ago.
I want to select some words out of an array of words.
For example, the array is like below:
var list = ['Brothers', 'Browsers', 'Dermatologist','Specialist','Optometry']
I use below script to perform selection
var pattern = "Der";
var matched = list.filter(a => a.indexOf(pattern) >= 0);
The matched variable will contains:
['Dermatologist']
But when I change the pattern variable value to "ist" the result would be
['Dermatologist','Specialist']
I want my filtering to works only matching from the beginning of every word. So if I set pattern variable value to "Bro" it will returns
['Brothers','Browsers']
But when I set pattern variable value to "ers" it will returns empty array
[]
Can anybody help?
You can simply use startsWith
var list = ['Brothers', 'Browsers', 'Dermatologist','Specialist','Optometry']
var pattern = "Der";
var matched = list.filter(a => a.startsWith(pattern));
console.log(matched)
You can use a regex with ^ (which means beginning of line) before the pattern:
var list = ['Brothers', 'Browsers', 'Dermatologist','Specialist','Optometry'];
var pattern = /^Bro.*/;
var matched = list.filter(a => pattern.test(a));
console.log(matched);
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 6 years ago.
I've got a couple strings and I need to pull characters out that appear between double quotes. The problem is, I want to grab them in groups.
var str = 'FF\"J"A4"L"';
var results = str.match(/\"(.*)\"/);
This returns everything between the first and last double quote. In this case it returns J"A4"L but what I need it to return is J and L.
The content between quotes is pretty much any unicode character like letters and numbers including as }, =, and #.
Any ideas on how to complete this with regex?
It sounds like the content between quotes is any character except for a quote, in which case you can get away with
/"([^"]*)"/
what you're looking for is this with the /g "global flag":
/("[^"]*")/g
In your example, it's like this:
var str = 'FF\"J"A4"L"';
var results = str.match(/("[^"]*")/g);
When doing this, results would be [""J"", ""L""], which contains the entire match (which is why the extra quotes are there).
If you wanted just the matched groups (which returns just the groups, not the whole match area), you would use exec:
var str = 'FF\"J"A4"L"';
var results = []
var r = /("[^"]*")/g
match = r.exec(str);
while (match != null) {
results.push(match[1])
match = r.exec(str);
}
Now, results is ["J", "L"]
This question already has answers here:
Call a function if a string contains any items in an array
(3 answers)
Closed 8 years ago.
I have a string in my JavaScript code (plain JavaScript, no jQuery or any other libs involved). And also I have an array which contains characters to be found in a string. I need to check if string contains any of those characters. Of course, it could be done with temporary variable like found and array elements iteration.
But is there any way to write nice and compact code? Just in case, I use ES5 (IE9+).
I want to achieve something like
var str = "Here is the string",
chars = ['z','g'];
if (str.containsAnyOf(chars)) {
...
}
What is the best way to write that piece of code?
You can use Array.prototype.some, like this
if (chars.some(function(c) { return str.indexOf(c) !== -1; })) {
// Atleast one of the characters is present
};
Consider using regular expression:
var str = "Here is the string",
chars = ['z','g'];
// constructs the following regexp: /[zg]/
if (new RegExp("[" + chars.join('') + "]").test(str)) {
alert("Contains!");
}