c# - JavaScript regex construction -


to test mathematical operator (*, /, +, -) exists in following items:

13*4 7+8 -15/3 7-5 

i have following regex javascript:

/[\*\+/\-]/ 

the problem regex identify negative sign in -15/3 operator. how make not match on first character in string?

you can match regex this:

one or more digits (also allowing decimal) followed optional whitespace  followed operator  followed optional whitespace  followed 1 or more digits (also allowing decimal) 

and reference captured group in middle operator:

/[\d.]+\s*([*+\-\/])\s*[\d.]+/ 

sample code:

var match = str.match(/[\d.]+\s*([*+\-\/])\s*[\d.]+/); if (match) {     var operator = match[1]; } 

working demo , test code: http://jsfiddle.net/jfriend00/dgrsg/


if want allow more digits before operators (such other expressions), have lot more simple or complex regex. answer assumes you're trying solve examples you've presented.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -