简体   繁体   English

有关Java中的RegExp的问题

[英]A Question About RegExp in Javascript

I wrote a regular expression 我写了一个正则表达式
^([+/-]?([0-9]+(.)?)|([0-9]*.[0-9]+))$ ^([+/-]([0-9] +())|?。?。([0-9] * [0-9] +))$
I create it by two ways 我用两种方法创建它

var _regex = "^([+/-]?([0-9]+(\.)?)|([0-9]*\.[0-9]+))$";
var _regexFloat = new RegExp(_regex);

and

var _regexFloat = /^([+/-]?([0-9]+(\.)?)|([0-9]*\.[0-9]+))$/ ;

the testing data is "1a" and "a1". 测试数据为“ 1a”和“ a1”。
at the second way, it work fine. 在第二种方法上,它可以正常工作。
but in the first way, it returns true. 但以第一种方式,它返回true。

Can anyone suggest me if I have something wrong. 如果我有什么问题,谁能建议我。

Thanks very much. 非常感谢。

Environment: 环境:

Windows Server 2003 Windows Server 2003
IE 6 IE 6

I believe you'll need to escape those backslashes in the string (in the first version). 我相信您需要在字符串中转义那些反斜杠(在第一个版本中)。

Try this: 尝试这个:

var _regex = "^([+/-]?([0-9]+(\\.)?)|([0-9]*\\.[0-9]+))$";
var _regexFloat = new RegExp(_regex);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM