0
inputStr = 'Hi My Name is ~Satish~ L ~Rathore~ , I live in ~Navi Mumbai~.'

Output Should be like each matching text between '~'

'Satish' 'Rathore' 'Navi Mumbai'

zhulien
  • 5,145
  • 3
  • 22
  • 36
  • should be similar: https://stackoverflow.com/questions/6323417/regex-to-extract-all-matches-from-string-using-regexp-exec – mech Dec 29 '21 at 10:49

1 Answers1

1

1) You can easily achieve the result using Regular expression as:

/~[\w\s]+~/g

enter image description here

const inputStr =
  'Hi My Name is ~Satish~ L ~Rathore~ , I live in ~Navi Mumbai~.';

const result = inputStr
  .match(/~[\w\s]+~/g)
  .map((s) => s.slice(1, s.length - 1));

console.log(result);

2) You can also do as:

/~[^~]+~/g

enter image description here

const inputStr =
  'Hi My Name is ~Satish~ L ~Rathore~ , I live in ~Navi Mumbai~.';

const result = inputStr.match(/~[^~]+~/g).map((s) => s.slice(1, s.length - 1));

console.log(result);
DecPK
  • 24,537
  • 6
  • 26
  • 42