0

I have a string "this is a test Meaning ID: 1 Status: Active lorem ipsum lorem ipsum". I want to retrive "1" between the two strings 'Meaning ID: ' and 'Status: '. The content between the two strings will change but the two strings themselves are static. I dont want to replace the content just want to retrive it into a variable.

I have tried using :

var meaningPattern = /Meaning ID: /g;
var statusPattern = /Status: /g;

and applied this to the original string and tried to get the substring but it just matches the pattern and does not return any substring.

var contentIWant = originalString.substring(meaningPattern, statusPattern);
Sarjerao Ghadage
  • 1,250
  • 13
  • 30
  • Just an advice, take a look on [Regex101](https://regex101.com/), there you can test the Regexes and check what they do and if it fits for you. – Gabriel Carneiro Jun 27 '18 at 14:21

3 Answers3

2

You could do a simple RegEx match for Meaning ID: (number), where (number) is a capture group.

var str = "this is a test Meaning ID: 17 Status: Active lorem ipsum lorem ipsum";

var rgx = /Meaning ID: ([0-9]*)/g;
var matches = rgx.exec(str);
var meaningId = +(matches[1]);  //Convert to int

console.log(meaningId);

RegEx allows you to get way more creative if need be, but given how specific your string is, I'm not sure you'll need it.

Tyler Roper
  • 20,917
  • 6
  • 31
  • 53
1

Although your question signals a lack of trying to read about how regexes work, here it goes:

The regex you're looking for is

/Meaning ID:\s*(.*)\s+Status/ - this is quite generic and I think that based on specifics about the values captured you can make it better and more efficient.

Try and readup on regexps and play around on websites like https://regex101.com/

AndreiS
  • 94
  • 2
0

This will return the string in between 'ID:' and 'Status'.

var str = 'this is a test Meaning ID: 1 Status: Active lorem ipsum lorem ipsum'
var mtc = str.match(/Meaning ID:(.*)Status/)
console.log(mtc[1])
Scath
  • 3,744
  • 10
  • 29
  • 39