-1

This javascript regex needs to extract "131PS" from "this is (131PS) for now".
Then I need to get "131" as a number and "PS" as a string.
Any suggestions? thx

 myString.match(/\(([^\)]+)\)/ig)[0]

returns (131PS) which is not what was expected.

Fred J.
  • 5,759
  • 10
  • 57
  • 106

1 Answers1

1

You need to work with capture regex groups () to withdraw the number and string separately, have a look:

let rawStr = "this is (131PS) for now";

let theMatch = rawStr.match(/\((\d+)([A-Z]+)\)/);

if (theMatch) {
  let theNum = parseInt(theMatch[1]);
  let theString = theMatch[2];
  
  console.log(theNum, theString);
}
Karen Grigoryan
  • 5,234
  • 2
  • 21
  • 35