RegEx for matching several occurrences after lookahead - javascript

I got following passage in a text file:
(default) AA / BBB)
ASDF / XYZ / GE
(default) CCCC)
I want to match all uppercase letters (2-4) after (default) to the closing bracket, so AA, BBB and CCCC are matched.
This is what i came up with, but it doesn't match the BBB:
(?<=default\)\s)[A-Z]{2,4}
So what am I missing to match more than one group of uppercase letters after (default)?

If we wish to only match the pattern in the question, we would just simply pass our desired cases and fail the other one using (default):
\(default\)(.+?([A-Z]{2,4}).+?([A-Z]{2,4})|.+?([A-Z]{2,4}))
Demo 1
or:
(?=\(default\))(.+?([A-Z]{2,4}).+?([A-Z]{2,4})|.+?([A-Z]{2,4})).+
Demo 2
const regex = /\(default\)(.+?([A-Z]{2,4}).+?([A-Z]{2,4})|.+?([A-Z]{2,4}))/gm;
const str = `(default) AA / BBB)
ASDF / XYZ / GE
(default) CCCC)`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
RegEx Circuit
jex.im visualizes regular expressions:

Related

How to write regular expression for getting text which is not in double quotes?

I have following text and pattern.
var str = 'Give 13234 "100" "1.2.3.4" %!';
var patt1 = /"(.*?)"/gm;
var result = str.match(patt1);
Result gives me the text which is in double quotes :
"100","1.2.3.4"
Is there any query which will give me the text which is not in double quotes?
Expected result is : Give 13234 %!
Instead of using a non greedy quantifier .*? you could use a negated character class "[^"]*" * to match the double quotes followed by 0+ spaces and replace that with an empty string.
If you don't want to match newline you can add the to the character class "[^\n"]*"
var str = 'Give 13234 "100" "1.2.3.4" %!';
var patt1 = /"[^"]*" */gm;
var result = str.replace(patt1, "");
console.log(result);
Here is a regexpr:
/".*?"|([^"]*)/g in group 1 we have the items looked for. Take into account that this regexpr will provide isolated spaces
const regex = /".*?"|([^"]*)/g;
const str = `Give 13234 "100" "1.2.3.4" %!`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
groupIndex === 1 && match && console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Definitely it's not a good practice to write regex like (.*?) , because you are matching anything.
I think this example is more cleaner:
var str = 'Give 13234 "100" "1.2.3.4" %!';
str.replace(/\s?"(.*?)"\s?/g,'');
and for the current case I would change the regex to:
/\s?"(\d+\.?){1,4}"\s?/g
optional match space
(match one or more digits and optional match dot){match the right side from 1 to 4 times}
optional match space

Getting string parameters using regex

I'm trying to get a regex that can extract data from
BAYARPLN ke 116160029354, SUKSES. Hrg: 84.822. SN: TGK IMUM M SAMIN/R1/450/MAR,APR/Rp.89222/Adm6000/977-1071/047421CA414149E5CEC5. Saldo: 7
and I want to find this value like this...
977-1071
I tried to using parameter regex link this
"/(Adm6000)([^\7]+)/"
But I cant find the string regex 977-1071. Can I ask for help for this?
Did you try like this? see regex https://regex101.com/r/tccJ42/1
const regex = /\d+\-\d+/g; //use \d{3}\-\d{4} if you've digit limit
const str = `BAYARPLN ke 116160029354, SUKSES. Hrg: 84.822. SN: TGK IMUM M SAMIN/R1/450/MAR,APR/Rp.89222/Adm6000/977-1071/047421CA414149E5CEC5. Saldo: 7`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
If you want to match 977-1071 after /Adm6000/ you could first match /Adm6000/ and then capture in a group not a forward slash one or more times ([^/]+)
\/Adm6000\/([^/]+)
Your value 977-1071 will be in captured group 1:
const regex = /Adm6000\/([^/]+)/;
const str = `BAYARPLN ke 116160029354, SUKSES. Hrg: 84.822. SN: TGK IMUM M SAMIN/R1/450/MAR,APR/Rp.89222/Adm6000/977-1071/047421CA414149E5CEC5. Saldo: 7`;
let match = regex.exec(str);
console.log(match[1]);

how to match '\u' codes for all the emojis with regexp?

I want to find match all the strings which will have emojis in the form of \u codes. I'm trying with below regexp but is not working.
/([\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2694-\u2697]|\uD83E[\uDD10-\uDD5D])/g
But, it is not detecting. I want to match and get
\ud83d\ude04\ud83d\ude04\ud83d\ude04\ud83d\ude04\ud83d\ude04\ud83d\ude04
these type of characters.
Well if you want to match emojis in the format \uXXXX using Regex, you can use this Regex:
/\\u[a-z0-9]{4}/gi
This is a simple Demo:
const regex = /\\u[a-z0-9]{4}/gi;
const str = `This is a pragraph \\ud83d having some emojis like these ones:
\\ude04
\\ud83d
\\ude04
Have you seen them?
\\ud83d\\ude04
Great!
`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
The regex you wrote won't work because you were not escaping the \.

Regex to capture code comments not working when * followed by /

I've made a regex expression to capture code comments which seems to be working except in the case when the comments contains * [anynumber of characters inbetween] /, e.g.:
/* these are some comments
=412414515/ * somecharacters /][;';'] */
Regex: (\/\*[^*]*[^/]*\*\/)
https://regex101.com/r/xmpTzw/2
\/\*[\s\S]*?\*\/
Just use a lazy operator instead of trying to not match *
For a start, I suggest this pattern:
(\/\*[\S\s]*?\*\/)
Demo
const regex = /(\/\*[\S\s]*?\*\/)/g;
const str = `This is/ some code /* these are some comments
=412414515/ * somechars / ][;';'] */*/
Some more code
/* and some more unreadable comments a[dpas[;[];135///]]
d0gewt0qkgekg;l''\\////
*/ god i hate regex /* asda*asd
\\asd*sd */`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}

Best way to get the word right before a certain word in javascript

I have the following string
this is the string and THIS is the word I want
I've tried using a regex for this:
var to_search = "is"
var regex = "/\S+(?="+to_search+")/g";
var matches = string.match(regex);
And I wanted matches to contain "THIS" (word that comes after the second if) but it does not seem to be working
Any idea? Thanks
regex101.com is a really great site to test your regex and it even generates the code for you.
const regex = /\bis.*(this)/gi;
const str = `this is the string and THIS is the word I want`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
First you have to double backslashes when using the string form of regexes.
Second, you forgot whitespace in your pattern:
var regex = new RegExp("\\S+\\s+(?="+to_search+")", "g");

Categories

Resources