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.
Related
I'm trying to manipulate a string that has tested as a positive match against my regex statement.
My regex statement is /\[table=\d](.*?)\[\/table] / gmi and an example of a positive match would be [table=1]Cell 1[c]Cell 2[/table]. I'm searching for matches within a certain div, which I'll call .foo in the code below.
However, once the search comes back saying it has found a match, I want to have the section that was identified as a match returned back to me so that I can start manipulating a specific section of it, namely count the number of times [c] appears and reference the number in [table=1].
(function(regexCheck) {
var regex = /\[table=\d](.*?)\[\/table] / gmi;
$('.foo').each(function() {
var html = $(this).html();
var change = false;
while (regex[0].test(html)) {
change = true;
//Somehow return string?
}
});
})(jQuery);
I'm quite new to javascript and especially new to RegEx, so I apologise if this code is crude.
Thanks for all of your help in advance.
Use exec instead of test and keep the resulting match object:
var match;
while ((match = regex[0].exec(html)) != null) {
change = true;
// use `match[0]` for the full match, or `match[1]` and onward for capture groups
}
Simple example (since your snippet isn't runnable, I've just created a simple one instead):
var str = "test 1 test 2 test 3";
var regex = /test (\d)/g;
var match;
while ((match = regex.exec(str)) !== null) {
console.log("match = " + JSON.stringify(match));
}
I would like to get special formatted strings ({string}) out of the HTML which are not inside a specific HTML tag.
For example I would like to match {test} and not <var>{test}</var>.
Therefore I am using the following regex: (excluding is done with ?!)
(?!<var>)\{\S+?\}(?!<\/var>)
So this works very well for texts with spaces, but if I have something like (where there is no space in-between):
<var>{name}</var>{username}
it matches two {}-strings: {name}</var>{username}
How can I just match {username} here?
Update:
If I need to do something like this
<var.*?<\/var>|(\{\S+?\})
How can I get the matched values, because the matched index depends on the position.
Examples:
Match 1:
"{username}<var>{name}</var>".match(/<var.*?<\/var>|(\{\S+?\})/g);
=> ["{username}", "<var>{name}</var>"]
Match 2:
"<var>{name}</var>{username}".match(/<var.*?<\/var>|(\{\S+?\})/g);
=> ["<var>{name}</var>", "{username}"]
Current Solution:
angular.forEach(html.match(regex), function (match) {
if(match.substring(0, 4) !== '<var') {
newAdded = match;
}
});
Is this really the 'best' solution for JavaScript?
Here is how you can achieve this using the following regex:
/<var.*?<\/var>|(\{\S+?\})/g;
var s = '<var>{name}</var>{username}<var>{newname}</var>{another_username}';
var log = [];
var m;
var regex = /<var.*?<\/var>|(\{\S+?\})/g;
while ((m = regex.exec(s)) !== null) {
if ( m[1] !== undefined) {
log.push(m[1]);
}
}
alert(log);
I generated the following code through a website. What I am looking for is that the script scans through a text variable against a set of keywords, and if it finds any of the keywords, it passes it to a variable. And if two keywords are found, both are joined by a hyphen and passed to a variable. I also need to set the "var str" dynamically. For instance, "var str == VAR10." VAR10 will have a dynamic text to be searched for keywords.
var re = /Geo|Pete|Rob|Nick|Bel|Sam|/g;
var str = 'Sam maybe late today. Nick on call. ';
var m;
if ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
}
In the above code, Sam and Nick are two keywords that I want hyphenated and passed to VAR10.
If two keywords are found, both are joined by a hyphen and passed to a
variable
Try this update to your original code for clarity:
var re = /Geo|Pete|Rob|Nick|Bel|Sam/g;
var str = 'Sam maybe late today. Nick on call. ';
var m;
var VAR10 = ""; // holds the names found
if ((m = re.exec(str)) !== null) {
var name1 = m;
if ((m = re.exec(str)) !== null) {
var name2 = m;
// Two names were found, so hyphenate them
// Assign name1 + "-" + name2 to the var that you want
VAR10 = name1 + "-" + name2;
} else {
// In the case only one name was found:
// Assign name1 to the var that you want
VAR10 = name1;
}
}
Note, change
var re = /Geo|Pete|Rob|Nick|Bel|Sam|/g;
to
var re = /Geo|Pete|Rob|Nick|Bel|Sam/g;
Here is an updated demo: http://jsfiddle.net/7zg2hnt6/1/
You can "capture" names with parenthesis:
/(Geo|Pete|Rob|Nick|Bel|Sam)/g
A sample: https://regex101.com/r/eK5hY2/1
To return the first two names found in hyphenated fashion:
str.match(re) . slice(0, 2) . join('-')
You have an extra | at the end of your regexp, which is likely to result in matches on an empty string. Remove it.
I also need to set the "var str" dynamically. For instance, "var str == VAR10." VAR10 will have a dynamic text to be searched for keywords.
var str == VAR10 is invalid syntax. I'll assume you mean var str = VAR10;. That's just a plain old variable assignment. All assignments in JS are "dynamic" by definition and happen at run-time. This would seem to have nothing to do with your specific problem.
Your code is almost doing what you want.
First you need to capture your matches, then join them.
http://jsfiddle.net/c6tjk21d/1/
var re = /(Geo|Pete|Rob|Nick|Bel|Sam)/g;
var str = 'Sam maybe late today. Nick on call. ';
var VAR10 = str.match(re).join('-')
console.log(VAR10);
I don't think you want to use exec because it maintains state and I've found it to be unintuitive. For example, in order to get more than one match with the code you've written, you'll need to loop through resulting on exec. Check out MDN for examples if you're interested. I almost always prefer match().
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
Well,
I've a JavaScript variable like below.
var myVar = '<p>This is ~ro..<strong>Yes it is ~hon no?</strong>. happy halloween ~yoo. <strong>~soon</strong> Ho ~no2 ~yes Ho Ho...~jik</p>';
I want to get all words after tilde (~) in a JavaScript array using regular expression or any other method. In the above case, the array result will be something like below.
["ro","hon","yoo","soon","no","yes","jik"]
The words can be of any length. Ultimately I want to get all words after ~ in a JavaScript array variable.
~([a-zA-Z]+)
try this.grab the captures.See demo.
http://regex101.com/r/yG7zB9/15
var re = /~([a-zA-Z]+)/gm;
var str = '<p>This is ~ro..<strong>Yes it is ~hon no?</strong>. happy halloween ~yoo. <strong>~soon</strong> Ho ~no2 ~yes Ho Ho...~jik</p>';
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.
}
Use the regex
~([a-zA-Z]+)
see how the regex matches http://regex101.com/r/uL5eX0/1
like this:
_list = myVar.match(/~(\w+)/g)
var myList = []
for( i in l){myList.push(l[i].split("~")[1])}
console.info(myList)
=======
update:
change regEx to:(thanks for #nu11p01n73R)
_list = myVar.match(/~([a-zA-Z]+)/g)