How to exclude certain string values via regex? [duplicate] - javascript

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+)/;

Related

How to create regex in javascript for an api uri to get query parameters with name and values when / is added before? [duplicate]

This question already has answers here:
Regex match everything after question mark?
(7 answers)
Closed 2 years ago.
Example API - /api/test/regex/?name=ciara
What i want - name=ciara
Try this:
(?<=\?name=)\w+
Regex demo
We cannot use capturing group here, because a repeated capture group will only store the last match that it found. Every time it makes a match, it will overwrite the previous match with the one it just found.
So, we need to go with 2 step solution, first we validate the URL, then use matchAll to find all references:
const regexp = /(?:(?:\&|\?)([^=]+)\=([^&]+))/g;
const str = '/api/test/regex/?name=ciara&p=q&r=s&t=u';
const array = [...str.matchAll(regexp)];
console.log(array);
This will give you:
> Array [Array ["?name=ciara", "name", "ciara"], Array ["&p=q", "p", "q"], Array ["&r=s", "r", "s"], Array ["&t=u", "t", "u"]]
Since we have an array now, we just need to iterate it to get all query params.
Following regex can be used validate existence of proper query strings:
\/?\?([^=]+)\=([^&]+)(?:\&([^=]+)\=([^&]+))*$

How do I insert something at a specific character with Regex in Javascript [duplicate]

This question already has answers here:
Simple javascript find and replace
(6 answers)
Closed 5 years ago.
I have string "foo?bar" and I want to insert "baz" at the ?. This ? may not always be at the 3 index, so I always want to insert something string at this ? char to get "foo?bazbar"
The String.protype.replace method is perfect for this.
Example
let result = "foo?bar".replace(/\?/, '?baz');
alert(result);
I have used a RegEx in this example as requested, although you could do it without RegEx too.
Additional notes.
If you expect the string "foo?bar?boo" to result in "foo?bazbar?boo" the above code works as-is
If you expect the string "foo?bar?boo" to result in "foo?bazbar?bazboo" you can change the call to .replace(/\?/g, '?baz')
You don't need a regular expression, since you're not matching a pattern, just ordinary string replacement.
string = 'foo?bar';
newString = string.replace('?', '?baz');
console.log(newString);

How to build a regex w/ new RegExp() [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 6 years ago.
Have an array of local storage keys that looks something like this
[
'$gl-user',
'$gl-date-preference::22'
'$gl-date-preference::28'
'$gl-mg-filters::22::1'
'$gl-mg-filters::22::8'
]
First ::_number_ represents the storeId.
Second ::_number_ can
represents any additional identifier.
Trying to build a function that takes a storeId and returns all keys that match that storeId. So if 22 was passed in to that function it would return
[
'$gl-date-preference::22',
'$gl-mg-filters::22::1',
'$gl-mg-filters::22::8'
]
Here is my first attempt. Copying this into the console returns null every time but I do not understand why.
var regex = new RegExp('^[$\w\d\-]+\:\:' + '22');
'$gl-mg-filters::22'.match(regex);
Any assistance in getting this regex to work, or ideas on a better solution would be greatly appreciated. Thank you!
Your regex isn't matching, because you're only escaping with the slashes inside of the string. Instead, you should be escaping it twice, e.g.:
var regex = new RegExp('^[$\\w\\d\\-]+\\:\\:' + '22');
'$gl-mg-filters::22'.match(regex);
Your initial attempt would try to compile ^[$wd-]+::22 into a Regex.

JavaScript RegExp matching group takes unwanted parentheses [duplicate]

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]);
}

Regular Expression only returning first result found [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I match multiple occurrences with a regex in JavaScript similar to PHP’s preg_match_all()?
I am trying to parse an xml document like this:
var str = data.match("<string>" + "(.*?)" + "</string>");
console.log(str);
I want to get all the elements between the [string] in an array but for some reason, it only returns the first string element found. Im not good with regular expressions so Im thinking this is just a small regex issue.
You want it to be global g
var str="<string>1</string><string>2</string><string>3</string>";
var n=str.match(/<string>(.*?)<\/string>/g);
//1,2,3
You have to form the RegEx adding a g to it like
/Regex/g

Categories

Resources