I have string in this format:
var a="input_[2][invoiceNO]";
I want to extract "invoiceNo" string. I've tried:
var a="input_[2][invoiceNO]";
var patt = new RegExp('\[(.*?)\]');
var res = patt.exec(a);
However, I get the following output:
Array [ "[2]", "2" ]
I want to extract only invoiceNo from the string.
Note: Input start can be any string and in place of number 2 it can be any number.
I would check if the [...] before the necessary [InvoiceNo] contains digits and is preceded with _ with this regex:
/_\[\d+\]\s*\[([^\]]+)\]/g
Explanation:
_ - Match underscore
\[\d+\] - Match [1234]-like substring
\s* - Optional spaces
\[([^\]]+)\] - The [some_invoice_123]-like substring
You can even use this regex to find invoice numbers inside larger texts.
The value is in capture group 1 (see m[1] below).
Sample code:
var re = /_\[\d+\]\s*\[([^\]]+)\]/g;
var str = 'input_[2][invoiceNO]';
while ((m = re.exec(str)) !== null) {
alert(m[1]);
}
You can use this regex:
/\[(\w{2,})\]/
and grab captured group #1 from resulting array of String.match function.
var str = 'input_[2][invoiceNO]'
var m = str.match(/\[(\w{2,})\]/);
//=> ["[invoiceNO]", "invoiceNO"]
PS: You can also use negative lookahead to grab same string:
var m = str.match(/\[(\w+)\](?!\[)/);
var a="input_[2][invoiceNO]";
var patt = new RegExp('\[(.*?)\]$');
var res = patt.exec(a);
Try this:
var a="input_[2][invoiceNO]";
var patt = new RegExp(/\]\[(.*)\]/);
var res = patt.exec(a)[1];
console.log(res);
Output:
invoiceNO
You could use something like so: \[([^[]+)\]$. This will extract the content within the last set of brackets. Example available here.
Use the greediness of .*
var a="input_[2][invoiceNO]";
var patt = new RegExp('.*\[(.*?)\]');
var res = patt.exec(a);
Related
So I have this (example) string: 1234VAR239582358X
And I want to get what's in between VAR and X. I can easily replace it using .replace(/VAR.*X/, "replacement");
But, how would I get the /VAR.*X/as a variable?
I think what you are looking for might be
string.match(/VAR(.*)X/)[1]
The brackets around the .* mark a group. Those groups are returned inside the Array that match creates :)
If you want to only replace what's in between "VAR" and "X" it would be
string.replace(/VAR(.*)X/, "VAR" + "replacement" + "X");
Or more generic:
string.replace(/(VAR).*(X)/, "$1replacement$2");
You can try use the RegExp class, new RegExp(`${VAR}.*X`)
You can store it as variable like this,
const pattern = "VAR.*X";
const reg = new RegExp(pattern);
Then use,
.replace(reg, "replacement");
If you
want to get what's in between VAR and X
then using .* would do the job for the given example string.
But note that is will match until the end of the string, and then backtrack to the first occurrence of X it can match, being the last occurrence of the X char in the string and possible match too much.
If you want to match only the digits, you can match 1+ digits in a capture group using VAR(\d+)X
const regex = /VAR(\d+)X/;
const str = "1234VAR239582358X";
const m = str.match(regex);
if (m) {
let myVariable = m[1];
console.log(myVariable);
}
Or you can match until the first occurrence of an X char using a negated character class VAR([^\r\nX]+)X
const regex = /VAR([^\r\nX]+)X/;
const str = "1234VAR239582358X";
const m = str.match(regex);
if (m) {
let myVariable = m[1];
console.log(myVariable);
}
here is my code:
var regEx = /^abcd(\d)+efg$/i;
var data = "abcd1234efg";
var match = regEx.exec(data);
the result is:
["abcd1234efg","4"]
But what I want is every single number: 1,2,3,4, not only 4
how do i get it ?
add another example:
var regEx = /^abcd(xy|z)+efg$/i;
var data = "abcdxyzefg";
var match = regEx.exec(data);
what i want is ["abcdxyzefg","xy,"z"]. especially "xy" and "z"
thx~~
you need to put the quantifier, the +, inside the group:
^abcd(\d+)efg$
https://regex101.com/r/P6qQUq/1
I have the following code:
https://jsfiddle.net/s3fdjyjv/1
var re = /11\B/g;
var str = '11111vjknvkjdnfvk11kjdnkjfgbnkfgjbnk1111jkngknfbkjf11111111gbnf';
console.log(str.match(re));
I am trying to get only the first set of consecutive matches.
So, in this example I just want to get the first consecutive matches of 11, which should result in the first 1111.
How can I get this result?
The /11\B/g regex matches multiple 11 substrings that are followed with an alphanumeric character or an underscore, as \B is a non-word boundary.
I am trying to get only the first set of consecutive matches.
For that, you need a capturing group and a backreference:
var re = /(11)\1+/;
or
var re = new RegExp("(11)\1+");
Note the /g global modifier must be removed since you only need the first match.
var re = new RegExp("(11)\\1+");
var str = '11111vjknvkjdnfvk11kjdnkjfgbnkfgjbnk1111jkngknfbkjf11111111gbnf';
var m = str.match(re);
var res = m ? m[0] + ", at " + re.lastIndex : "";
document.body.innerHTML = res;
I need to match all entries like { * } for string (using Javascript). How can I do it?
Here is an input: "#SD#{date};{time};{lat1};{lat2};{lon1};{lon2};{speed};{course};{height};{sats}\r\n";
thanks in advance
Using regular expression maybe:
var r = /{.*?}/g;
var s = "#SD#{date};{time};{lat1};{lat2};{lon1};{lon2};{speed};{course};{height};{sats}\r\n";
var matches = s.match(r);
Or if you want to strip those {} in results, you can run an .exec() loop to get a captured data only:
var r = /{(.*?)}/g;
var s = "#SD#{date};{time};{lat1};{lat2};{lon1};{lon2};{speed};{course};{height};{sats}\r\n";
var matches = [];
var match = null;
while(match = r.exec(s)) {
matches.push(match[1]);
}
You can try with the following regular expression:
var str = "#SD#{date};{time};{lat1};{lat2};{lon1};{lon2};{speed};{course};{height};{sats}\r\n".
var m = str.match(/{.*?}/g);
You'll find all the matches inside m.
Further references: http://www.w3schools.com/jsref/jsref_match.asp
For just the text within the brackets, you should be able to use this pattern:
var pattern = new RegExp("([^{|^}]+)(?=})", "g");
var testString = "#SD#{date};{time};{lat1};{lat2};{lon1};{lon2};{speed};{course};{height};{sats}\r\n";
var bracketTextArray = testString.match(pattern);
The bracketTextArray variable will be an array that contains the following values:
"date", "time", "lat1", "lat2", "lon1", "lon2", "speed", "course", "height", "sats"
The regex matches every occurrence of one or more of any character except { and } (that's this part: ([^{|^}]+)), that are immediately followed by a } character (that's this part: (?=})). The "g" in the RegExp definition makes the pattern "greedy", so that it will find all occurrences.
i have a string like this .
var url="http://localhost/elephanti2/chaink/stores/stores_ajax_page/5/b.BusinessName/asc/1/11"
i want to get substrings
http://localhost/elephanti2/chaink/stores/stores_ajax_page
and
5/b.BusinessName/asc/1/11
i want to split string from the 7 th slash and make the two sub-strings
how to do this ??,
i looked for split()
but in this case if i use it i have to con-cat the sub-strings and make what i want . is there a easy way ??
try this one:
var url="http://localhost/elephanti2/chaink/stores/stores_ajax_page/5/b.BusinessName/asc/1/11";
var parts = url.split('/');
var p1 = parts.slice(0,6).join('/');
var p2 = parts.slice(7).join('/');
alert(p1);
alert(p2);
p1 should get the first part and p2 is the second part
You can try this regex. Generally if your url pattern always follow this structure, it will work.
var pattern = /(\w+:\/\/(\w+\/){5})/i;
var url = "http://localhost/elephanti2/chaink/stores/stores_ajax_page/5/b.BusinessName/asc/1/11";
var result = url.split(pattern);
alert(result[1]);
alert(result[3]);
Try this :
var str = 'http://localhost/elephanti2/chaink/stores/stores_ajax_page/5/b.BusinessName/asc/1/11',
delimiter = '/',
start = 7,
tokens = str.split(delimiter).slice(start),
result = tokens.join(delimiter);
var match = str.match(/([^\/]*\/){5}/)[0];
Find this fiddle