I tried with this
var regex = /.*<\s+(.*)\s+>.*/g;
but it is not working
Sample string:
your call average is <result.x> OK. <result.y> <result.b>
I want all strings between <
and >
using regex
Thanks in advance,
I tried with this
var regex = /.*<\s+(.*)\s+>.*/g;
but it is not working
Sample string:
your call average is <result.x> OK. <result.y> <result.b>
I want all strings between <
and >
using regex
Thanks in advance,
use below regex for extract the result.
var regex = /<(.*?)>/g;
var myStr = 'your call average is <result.x> OK. <result.y> <result.b>';
var arr = [];
var regex = /<([^>]+)>/g;
var match;
while( match = regex.exec( myStr ) ) {
arr.push( match[1] );
}
console.log(arr);
You can use a regex like /\<([^<>]*)\>\/g
. You can find explanation at https://regex101.com/r/DOR7C7/1. It will match all the characters except <
or >
inside <..>
and capture the results.
JavaScript Code
let str = `your call average is <result.x> OK. <result.y> <result.b>`;
let re = /\<([^<>]*)\>/g;
let res = re.exec(str);
let results = [];
while(res){
results.push(res[1]);
res = re.exec(str);
}
console.log(results);
This should work:
/<\s*(.*?)\s*>/g
Group 1 is the string you want. Online Demo.
const regex = /<\s*(.*?)\s*>/gm;
const str = `your call average is <result.x> OK. <result.y> <result.b>`;
let m;
while ((m = regex.exec(str)) !== null) {
console.log(m[1])
}
Note that you can optionally enable the s
option to match <>
that spans multiple lines.
Explanation:
The .*
at the start and end of your regex is not needed because you are not trying to match anything outside of the <>
. This is why your regex should start with <
and ends with >
. After matching some whitespaces, you start to capture the things inside the <>
with (.*?)
. Note that I used a lazy quantifier here. This is so that it stops matching as soon as it sees a >
. For more info, see here.
var d = "your call average is <result.x> OK. <result.y> <result.b>".split('>')
var result = []
d.forEach(function(te) {
if (!te.trim()) return
var t = te.split('<').pop()
if (!t.trim()) return
result.push(t)
})
console.log(result)