admin管理员组文章数量:1410712
Im want to remove all but the float in this sting:
string = "1 south african rand is 0.11044"
Im doing it like this:
reg = /[^\d+.\d+]/g
console.log string.replace(reg, '')
that logs
10.11044
that is wrong, I want only the xxxx.xxxxx part. 1 is not a float so it should not be part of this?
How should I chage it?
Im want to remove all but the float in this sting:
string = "1 south african rand is 0.11044"
Im doing it like this:
reg = /[^\d+.\d+]/g
console.log string.replace(reg, '')
that logs
10.11044
that is wrong, I want only the xxxx.xxxxx part. 1 is not a float so it should not be part of this?
How should I chage it?
Share Improve this question asked Apr 16, 2013 at 9:42 HarryHarry 13.3k30 gold badges112 silver badges169 bronze badges 3-
1
[^....]
matches any individual character that isn't one of the ones inside the brackets. It's not looking for sequences. – Barmar Commented Apr 16, 2013 at 9:47 - The matching bit works great, just not with the [] yes.. – Harry Commented Apr 16, 2013 at 9:48
- I've corrected my answer using a negative look-around. Tested, works correctly my side. – Evan Knowles Commented Apr 16, 2013 at 9:50
7 Answers
Reset to default 4Try this instead
^((?!\d+\.\d+).)*
See this answer for more details: Regular expression to match a line that doesn't contain a word?
Following regex should match floats for you (for the example provided):
/(-?\d*\.\d+)/
To replace:
console.log (string.replace(/(-?\d*\.\d+)/, ''));
I have used positive look behind (?<=..) in below regular expression
\.\d+(?<=\d)
Use this regular expression and replace below value with ''. The result will be 1 23 33 3
1 23 33.2000 3.4445
Hope it helps
var reg = /\d+\.\d+/g
var str = "1 south african rand is 0.11044";
var onlyFloats = str.match(reg).join(" ");
console.log(onlyFloats)
Try this
string = "1 south african rand is 0.11044"
reg = /(\d+\.\d+)/g;
string.match(reg);
/**
* @param $string
* @return string
*
* output
* a.a.a.a.a.a. => 0.0
* 1.1.1.1.1.1. => 1.11111
* 2a$2.45&.wer.4 => 22.454
*/
function stringToFlout($string) {
$parts = explode('.', $string);
for ($i = 0; $i < count($parts); $i++) {
$parts[$i] = preg_replace('/[^0-9]/', '', $parts[$i]);
}
$left = $parts[0];
if (empty($left))
$left = 0;
$float[] = $left;
unset($parts[0]);
$right = implode('', $parts);
if (empty($right))
$right = 0;
$float[] = $right;
return implode('.', $float);
}
I would use this regex:
(0|([1-9][0-9]*))?\.(0|([1-9][0-9]*))
The (0|([1-9][0-9]*))?
matches an optional integer part, the \.
matches dot, and the final (0|([1-9][0-9]*))
matches a mandatory fractional part.
It succeeds for:
.5
0.5
0.0
0.1
0.5foo
31.41592
It fails for:
00.5
100
nice
2.
Anchored version:
^(0|([1-9][0-9]*))?\.(0|([1-9][0-9]*))$
本文标签: javascriptRegex issueReplace floatsnot intStack Overflow
版权声明:本文标题:javascript - Regex issue - Replace floats, not int - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744846969a2628266.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论