正则表达式不起作用 [英] Regex Not working

查看:142
本文介绍了正则表达式不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从REGEXlib获得了这个正则表达式,基本上它检查是否它是一个有效的英国DVLA号码牌,但是我不能在JavaScript内使用它,有些人可以帮助我,因为它不是最好的正则表达式。

  //在文本框板文本的更改事件中
函数setValue(target){
var platetext = target.value;
$('#numberplateyellow')。empty();
$('#numberplatewhite')。empty();


var plateregex = /([A-HJ-PR-Y] {2}([0] [1-9] | [1-9] [0-9]) | [A-HJ-PR-Y] {1}([1-9] | [1-2] [0-9] | 30 | 31 | 33 | 40 | 44 | 55 | 50 | 60 | 66 | 70 | 77 | 80 | 88 | 90 | 99 | 111 | 121 | 123 | 222 | 321 | 333 | 444 | 555 | 666 | 777 | 888 | 999 | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900))[] [A-HJ-PR-Z] {3} $ /;

if(document.platebuilder.target.value.search(plateregex)== - 1){

var answer = window.confirm(Non LEGAL Plate Detected(YES我们会在TOM中显示同意信箱)\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

if(answer){
$('#numberplateyellow')。append(platetext);
$('#numberplatewhite')。append(platetext);
}

}

}

感谢

解决方案

您正在混合使用jQuery和标准脚本。



更改 var platetext = target.value;



to



var platetext = target.val();





if(document.platebuilder.target.value.search(plateregex)== - 1){





$ {


I got this regex expression from REGEXlib and basically it checks to see if it is a valid UK DVLA Number Plate however i cannot get it to work within JavaScript could some one please help as im not the best with regex as it is.

//On change event for the textbox plate text
function setValue(target){
        var platetext = target.value;
        $('#numberplateyellow').empty();
        $('#numberplatewhite').empty();


        var plateregex = /([A-HJ-PR-Y]{2}([0][1-9]|[1-9][0-9])|[A-HJ-PR-Y]{1}([1-9]|[1-2][0-9]|30|31|33|40|44|55|50|60|66|70|77|80|88|90|99|111|121|123|222|321|333|444|555|666|777|888|999|100|200|300|400|500|600|700|800|900))[ ][A-HJ-PR-Z]{3}$/;

        if (document.platebuilder.target.value.search(plateregex)==-1){

        var answer = window.confirm ("Non LEGAL Plate Detected (YES WE WILL SHOW THE AGREE BOX ONCE I HAVE IT FROM TOM) \n\n Do you understand that this is now classed as a show plate ?");

        if (answer) {
        $('#numberplateyellow').append(platetext);
        $('#numberplatewhite').append(platetext);
        }

      }

}

Thanks

解决方案

You are mixing jQuery and standard script.

Change var platetext = target.value;

to

var platetext = target.val();

and

if (document.platebuilder.target.value.search(plateregex)==-1){

to

if (!platetext.match(plateregex)) {

这篇关于正则表达式不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆