1

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,

Alex Taylor
  • 8,343
  • 4
  • 25
  • 40
Praveen R
  • 76
  • 6

4 Answers4

1

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);
Shiv Kumar Baghel
  • 2,464
  • 6
  • 18
  • 34
0

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);
vibhor1997a
  • 2,336
  • 2
  • 17
  • 37
0

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.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

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)
  • Without Regular Eaxpression You can do.if you are going by Regular Expression you need to match manything.so go by normal split –  Sep 21 '18 at 07:00