javascript - Regex to find a single dot in a string -
is there way find single standing dot in string regex? example, user input is:
7. + 6.5
is there way observe there's unnecessary dot in string 7 , remove while not touching dot in 6.5?
also, there way observe multiple occurrences of same operator next considered invalid:
7 ++++ 5 --- 6
any hint appreciated.
thanks.
to observe multiple occurence of operator regex can be
([^\d\s])\1+
repalce string $1
see link http://regex101.com/r/ln3wh5/1
var re = /([^\d\s])\1+/gm; var str = '7 ++++ 5 --- 6 + 5'; var subst = '$1'; var result = str.replace(re, subst);
will produce output as
7 + 5 - 5 +5
the single standing .
can matched using negative positive lookahead (?! )
using regex
\.(?!\d)
the matched part can see @ http://regex101.com/r/tc7ro6/1
edit
to validate string contain single operator,
regex : ^\d+(\s*[+-\/*%]\s*\d+)+$
test : http://regex101.com/r/ln3wh5/3
javascript regex
No comments:
Post a Comment