I always have a hard time with regex..
I'm trying to select the text between (taking into acount the before and after)
'window.API=' and ';' //for window.API= '--API--';
and other cases like:
'window.img_cdn=' and ';' //for window.img_cdn= '--imgCDN--';
any tips on witch regex concepts I should use would be a great help!
If you want to capture the content between 'xx' you can use a regex like this:
'(.*?)'
working demo
For the sample text:
window.API= '--API--';
window.img_cdn= '--imgCDN--';
You will capture:
MATCH 1
1. [13-20] `--API--`
MATCH 2
1. [40-50] `--imgCDN--`
The javascript code you can use is:
var re = /'(.*?)'/g;
var str = 'window.API= \'--API--\';\nwindow.img_cdn= \'--imgCDN--\';';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
On the other hand, if you specifically want to capture the content for only those entries, then you can use this regex:
window\.(?:API|img_cdn).*?'(.*?)'
If you want to match any text between a <some string>= sign and a semicolon, here you go:
(?:[\w\.]+\s*=\s')(.+)(?:';)$
This regex pattern will match a full string if an escaped apostrophe is present in the string: //for window.img_cdn = '--imgCDN and \'semicolon\'--';
JavaScript code:
var re = /(?:[\w\.]+\s*=\s')(.+)(?:';)$/gm;
var str = '//for window.img_cdn= \'--imgCDN--\';\n//for window.img_cdn = \'--imgCDN and semicolon = ;;;--\';';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// view results
}
The required text is in the 1st captured group. In case there is a semicolon in the text you are looking for, you will correctly match it due to the $ anchor.
See demo here
Related
I have the following dynamic string:
[TITLE|prefix=a] [STORENAME|prefix=s|suffix=s] [DYNAMIC|limit=10|random=0|reverse=0]
And I would like to get the value a from [TITLE|prefix=a] and the value s from [STORENAME|prefix=s|suffix=s].
To get the prefix value of TITLE, I have tried it with result.match(/prefix=*\||\]/) but I'm not getting what I need.
You could start by matching [ and uppercase chars A-Z. then match |prefix followed by capturing in a group what you want to keep.
Then optionally match |suffix and use another group to capture what you want to keep.
\[[A-Z]+\|(prefix=)([^\]|]+)(?:\|(suffix=)([^\]|]+))?]
Regex demo
const regex = /\[[A-Z]+\|(prefix=)([^\]|]+)(?:\|(suffix=)([^\]|]+))?]/g;
const str = `[TITLE|prefix=a] [STORENAME|prefix=s|suffix=s] [DYNAMIC|limit=10|random=0|reverse=0]`;
let m;
while ((m = regex.exec(str)) !== null) {
console.log(`key: ${m[1]}`)
console.log(`value: ${m[2]}`)
if (m[3] !== undefined) {
console.log(`key: ${m[3]}`)
console.log(`value: ${m[4]}`)
}
}
If [TITLE has to be on the left, you might also use a positive lookbehind with an infinite quantifier to get the matches:
(?<=\[TITLE\|.*)(prefix=|suffix=)([^|\]*])
Regex demo
const regex = /(?<=\[TITLE\|.*)(prefix=|suffix=)([^|\]*])/g;
const str = `[TITLE|prefix=a] [STORENAME|prefix=s|suffix=s] [DYNAMIC|limit=10|random=0|reverse=0]`;
let m;
while ((m = regex.exec(str)) !== null) {
console.log(`key: ${m[1]}`)
console.log(`value: ${m[2]}`)
}
Try below:
(?<=TITLE\|prefix=)[\w]+|(?<=[\w]+\|suffix=)[\w]+
Explanation:
TITLE|prefix= --> Will search for text "TITLE|prefix="
?<= --> will deselect it
(?<=[\w]+|suffix=) --> Similarly, will select "suffix=" and anything before it, and will deselect it
[\w]+ --> will select word after "TITLE|prefix=" and "suffix="
You could try following regex.
(?<=prefix=).*?(?=]|\|)
Details:
(?<=prefix=): Lookbehind - matches string after the characters prefix=
.*?: matches any characters as few as possible
(?=]|\|): gets any characters until ] or |
I also tried to run code on javascript.
var string = "[TITLE|prefix=a] [STORENAME|prefix=s|suffix=s] [DYNAMIC|limit=10|random=0|reverse=0]";
var res = string.match(/(?<=prefix=).*?(?=]|\|)/g);
console.log(res);
Sorry, probably being dumb this morning, but I don't know much about regular expressions, but have created something I want to use with https://regex101.com/
But... I can't use the code they suggest in Javascript without escaping it first.
Here's the regex: (?<=color:\s)([a-z]+)
Which, does what I want (matching a word after color: in a CSS file)
But, the code they suggest to use in JS is:
var re = /(?<=color:\s)([a-z]+)/g;
var str = ' color: black';
var m;
while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
The first line, won't work, so I escaped it to: var re = /\(?<=color:\s\)([a-z]+)/i which stops the Javascript error, but won't match the strings any more.
What am I doing wrong?
As an aside... can anyone point me to expanding this regex to exclude anything followed by a bracket? I am trying to get color names only, so "color: black;" should match, also "box-shadow: black... etc" should match, but ideally, not "color: rgb(... etc"
It is true that JS does not support look-behinds, although there are workarounds:
Reverse the string and then matches that enables using look-aheads
Use capturing groups
Use a while loop with re.lastIndex manipulation
In this case, it is much easier to use the capturing group:
var re = /\bcolor:\s*([a-z]+)/ig;
var str = ' color: black';
var m;
while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// m[1] is holding our value!
document.getElementById("res").innerHTML = m[1];
}
<div id="res"/>
I need a javascript regex pattern to test a schema variable, so that it should have either of the following.
It can start with any character followed by "_water_glass" and must not be anything after water_glass like "xxxx_water_glass"
or
It can be just "water_glass" not necessary to have character before water_glass and must not be anything after water_glass.
Could anyone help on this please to get the regex pattern.
Try this simply /^.*_?\_water_glass/
var re = /^.*_?_water_glass/mg;
var str = 'horse.mp3_country_code\n4343434_country_code\n_country_code';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
DEMO https://regex101.com/r/gB9zL7/2
Here you are:
^(?:.+_|)water_glass$
Details:
^- start of string
(?:.+_|) - an optional 1+ chars other than line break chars, as many as possible, up to the last _ including it
water_glass - a water_glass substring
$ - end of string.
See this regex demo and a demo code below:
var re = /^(?:.+_|)water_glass$/gm;
var str = 'xxxx_water_glass\nwater_glass';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
I am trying to extract data from a string using RegEx , but i am getting a NULL value as result.
here is my current code
var re = /(\[cid=(?:[0-9]*)(?:(?:,\[[^]]*\][^]]*)?|(?:,[^]]*))\])/;
var str = '[cid=5555,[CONSTIMG]5555.jpg]The Sample text is awesome';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
}
console.log(m[0]);
The value that i am getting back is NULL.
If anyone can point me in the right direction, that would be very helpful.
Thanks in advance.
Your expression matches PCRE regular expression syntax but not JavaScript because JavaScript requires that square brackets inside a character class be escaped with \. This is what you want:
(\[cid=(?:\d*)(?:(?:,\[[^\]]*\][^\]]*)?|(?:,[^\]]*))\])
Explained: https://regex101.com/r/pN4vP4/2
Hello where is some text patterns like:
some text Here +0.25(2)
some text Here 0.25(2.3)
some text Here 0.00(2.3)
some text Here -1.5(1.5)
...
some text Here param_1(param_2)
I need to extract two values param_1 and param_2. How to solve it using regexpressions? (needed Javascript)
param_1 is number contais +, - or nothing perfix.
param_2 is number
([+-]?\d+(?:\.\d+)?)\((\d+(?:\.\d+)?)(?=\))
Try this.See demo.Grab the captures.
https://regex101.com/r/vD5iH9/25
var re = /([+-]?\d+(?:\.\d+)?)\(\d+(?:\.\d+)?(?=\))/g;
var str = 'some text Here +0.25(2)\nsome text Here 0.25(2.3)\nsome text Here 0.00(2.3)\nsome text Here -1.5(1.5)\n...\nsome text Here param_1(param_2)';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
EDIT:
(.*?)\s([+-]?\d+(?:\.\d+)?)\((\d+(?:\.\d+)?)(?=\))
use this to capture all three components.See demo.
https://regex101.com/r/vD5iH9/28
I assume that you would like an array of param_1s, and then an array of param_2s.
You can accomplish this with two simple regex's:
(to capture param_1s):
/[+-\d.]+(?=\([\d.]+\)$)/gm
param_1 demo
and param_2's are even simpler:
/[\d.]+(?=\)$)/gm
Try the full jsFiddle demo.
var param1 = str.match(/[+-\d.]+(?=\([\d.]+\)$)/gm);
var param2 = str.match(/[\d.]+(?=\)$)/gm);
param1 is now an array containing param_1s, and param2 for param_2s.