Parser For String
Suppose i have a string, str = 'Date()+Abs(9)Day()+45' I want an array that contains: arr[0]=Date() arr[1]=Abs(9) arr[2]=Day() My current parser: var temp = str.match(/\w+(\
Solution 1:
This is a pretty easy expression.
/ -- START expression
\w+ -- One or more alpha-numeric (plus '_')
\( -- Literal opening parenthesis
[\w/-]* -- Zero or more alpha-numeric (plus '_', '/', and '-')
\) -- Literal closing parenthesis
/g -- END expression; Global match
var str = "Date(20/04/98)+Abs(-9)Day(20/04/98)+45"var arr = str.match(/\w+\([\w/-]*\)/g);
console.log(JSON.stringify(arr, null, 2));
// arr[0] = Date(20/04/98)// arr[1] = Abs(-9)// arr[2] = Day(20/04/98)
Learn more
- Information
- Tools
- RegExr (Recommended)
- Dan's Tools - Regex Pal (same as Regex Tester)
- regular expressions 101
Post a Comment for "Parser For String"