I need to match the following values and my current Regular Expression
can't seem to get %
values to be matched or numbers without -
and $
Values I need a matched:
$123.45 - Match
-$123.45 - Match
123.45 - Needs Matched
-123.45 - Match
.99 - Needs Matched
-.99 - Match
7% - Needs Matched
-7% - Needs Matched
500 - Needs Matched
Regular Expression:
^[-$][$]?\d*(?:\.\d{0,2})?$
I need to match the following values and my current Regular Expression
can't seem to get %
values to be matched or numbers without -
and $
Values I need a matched:
$123.45 - Match
-$123.45 - Match
123.45 - Needs Matched
-123.45 - Match
.99 - Needs Matched
-.99 - Match
7% - Needs Matched
-7% - Needs Matched
500 - Needs Matched
Regular Expression:
^[-$][$]?\d*(?:\.\d{0,2})?$
Share
Improve this question
edited Jun 20, 2020 at 9:12
CommunityBot
11 silver badge
asked Dec 6, 2011 at 16:53
Zach LZach L
1,2754 gold badges18 silver badges37 bronze badges
4 Answers
Reset to default 6Here you go:
/^-?\$?[0-9]*\.?([0-9]{2})?%?$/
$str = "
$123.45 - Match
-$123.45 - Match
123.45 - Needs Matched
-123.45 - Match
.99 - Needs Matched
-.99 - Match
7% - Needs Matched
-7% - Needs Matched
500 - Needs Matched
";
preg_match_all('#-?[$]?[0-9]*\.?[0-9]{2}+[%]?#i',$str, $res);
print_r($res);
This casts the web a little to wide though as it finds -$123.12% to be a number
/^-?\$?[0-9]*(\.?[0-9]{1,2})?%?$/
I adapted on akellehe's answer to match a range of currency symbols:
var regex = new RegExp("^-?[^0-9]?[0-9]*[0-9\u002e]+%?$");
and to capture the output for more analysis:
var regex = new RegExp("^(-?)([^0-9]?)([0-9]*[0-9\u002e]+)(%?)$");
// $1 captures the negative sign,
// $2 captures the currency symbol
// $3 captures the number
// $4 captures the percent sign
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744785109a4593576.html
评论列表(0条)