参考语法 : http://blog.didispace.com/regular-expression-1/
工具生成各种语言 :http://tools.jb51.net/regex/create_reg
语言 : js
需求 : 获取正负数的带有小数点的数字字符
var pattern = /(-)?\d+[//.]\d+/;
var str = "$-1234.34";
var result=str.match(pattern);
if(result && result.length>0){
//判断下然后才显示比较好
console.log(result[0]);
}
测试结果:
var str = "$1234.34";
VM763:6 1234.34
var str = "$-1234.34";
VM766:6 -1234.34
截取字符操作: replace
var str="¥12342.5";
var result=matchMoney(str);
console.log("结果为 : ",result);
function matchMoney(money){
var arr=["¥","¥","$","€","R$","£"];
var result = money;
for(var k in arr){
if(money && money.indexOf(arr[k])==0){
result=str.replace(arr[k],"");
break;
}
}
return result;
}
replaceAll例子:
var str="¥¥¥¥12342.5 - ¥¥¥¥45.89";
var result=matchMoney(str);
console.log("结果为 : ",result);
function matchMoney(money){
var arr=["¥","¥","$","€","R$","£"];
var result = money;
for(var k in arr){
if(money && money.indexOf(arr[k])> -1){
//result=str.replace(arr[k],"");
result=result.replace(new RegExp(arr[k],"gm"),"");
}
}
return result;
}
replaceALL方法:
String.prototype.replaceAll = function(s1,s2){
return this.replace(new RegExp(s1,"gm"),s2);
}