Reputation: 1345
regex always making me struggle.. what i want here is i have to check whether a string is in the given format in js.
{word}/numbers operator {word}/numbers operator {word}/numbers
Eg : {VAT}+50+{tax}-20
Note :
words always should be wrapped with {}
(curly brackets)
number of operations is unlimited
and it will be more appreciated if someone suggest any website or something that i can get easy with regex
in future.. thanku :)
Upvotes: 0
Views: 73
Reputation: 32199
The following regex
also works for any length:
^((\{\[a-zA-Z0-9]\}|\d+)([+\-\*\/]|$))+$
Demo: http://regex101.com/r/hV1hL1
Upvotes: 1
Reputation: 152276
Just try with following regex:
/^((?:{[a-z]+})|\d+)([+*/-]((?:{[a-z]+})|\d+))*$/i.test("{VAT}+50+{tax}-20")
In this regex are included following operators: +*/-
Upvotes: 4