简体   繁体   English

正则表达式模式匹配问题

[英]Regular Expression Pattern matching issue

Not a lot of experience in RegEx stuff. 在RegEx方面经验不足。

I have the following in java script which works perfectly fine. 我在Java脚本中具有以下功能,效果很好。

The following pattern is used allow only alpha numeric 使用以下模式仅允许字母数字

var valid = /^[A-Za-z0-9]+$/.test("a"); // returns true
var valid = /^[A-Za-z0-9]+$/.test("@"); // returns false

I am using the pattern part "^[A-Za-z0-9]" in some other places of the code and was asked to use the part "^[A-Za-z0-9]" in a variable and use it so that it is not repetitive. 我在代码的其他位置使用模式部分“ ^ [A-Za-z0-9]”,并被要求在变量中使用部分“ ^ [A-Za-z0-9]”并使用它因此它不是重复的。 The following is a modification to the above: 以下是对上述内容的修改:

var regExPart= "^[A-Za-z0-9]";
var regExString = ("/" + regExPart+ "+$/".replace(/\"/g, "")); // replacing the quotes
var regExp = new RegExp(regExString); // results in /^[A-Za-z0-9]+$/
var valid = regExp.test(charValue); // charValue could be any keyvalue "a" or "@"

//the above returns false for "a" 
//the above returns false for "@"

I am writing this in a keypress event to allow only alpha numeric 我在按键事件中写这个,只允许字母数字

keypressValidation: function (e) {
var charCode = (e.which) ? e.which: event.keyCode;
var charValue = String.fromCharCode(charCode);
var valid = return /^[A-Za-z0-9]+$/.test(charValue);
if (!valid) 
{
    //prevent default (don't allow/ enter the value)
}

Not sure why. 不知道为什么。 What am I missing in this. 我在此缺少什么。 Need to return true for "a" and false for "@" for both the approaches. 两种方法都需要为“ a”返回true,为“ @”返回false。 Any help/ suggestion would be of great help. 任何帮助/建议都会有很大帮助。 Thank in advance. 预先感谢。

For the RegExp class constructor, you do not need to specify forward slashes / . 对于RegExp类构造函数,您无需指定正斜杠/

 var regExPart= "^[A-Za-z0-9]"; var regExp = new RegExp(regExPart + "+$"); // results in /^[A-Za-z0-9]+$/ console.log('a', regExp.test('a')) console.log('@', regExp.test('@')) 

It is not a must to contain '/'s in regexp regexp中不必包含“ /”

new RegExp("^[0-9a-zA-Z]$").test('a')

return true 返回true

new RegExp("^[0-9a-zA-Z]$").test('@')

return false 返回false

So just do 所以做

var rex="^[0-9a-zA-Z]$"

And you can use it anywhere. 您可以在任何地方使用它。 Tested in Chrome console. 已在Chrome控制台中测试。

I've made an example using your regex of what it should do, i think the way you were building your regex was not helping. 我已经使用您的正则表达式做了一个例子,我认为您构建正则表达式的方式无济于事。 You don't need to create a string and then create a new regex object , you can use /regex part/ . 您无需创建字符串然后创建新的regex对象,可以使用/regex part/ Anyways here is a working example. 无论如何,这是一个有效的示例。

 function keypress(e) { // Get the current typed key var keynum = e.key; // this regex only allow character between a and z and 0 and 9 var regex = /^[a-zA-Z0-9]+$/; // we check if the current key matches our regex if(!keynum.match(regex) ) { // it doesn't ? well we stop the event from happening e.preventDefault(); } } 
 <input type="text" onkeypress="keypress(event)"> 

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

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