2

I have this string and I Want to extract strings that are between ^ but without the ^ itself in javascript:

 const input = "^k^ 989 ^s^"

 var ptrn = /\^(.*?)\^/mg;
 var results = input.match(ptrn);
 console.log(results)  ==> ["^k^" , "^s^]

the following with exec has a different result but I prefer the match method than the loop of exec to get all matches

 let results = ptrn.exec(input);
 console.log(results) ==> ["^k^", "k"]
Wede Asmera Tseada
  • 513
  • 2
  • 4
  • 14
  • Have you tried using [`String.split()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)? – axiac Jul 29 '20 at 11:25

1 Answers1

0

as another way, you can use split to achieve that.

const input = "^k^ 989 ^s^";
const results = input
  .split('^')
  .filter((_, index) => index % 2 === 1);
console.log(results);
Kavian Rabbani
  • 934
  • 5
  • 15