简体   繁体   English

正则表达式匹配JavaScript中的特定字符串

[英]regex match specific string in javascript

I need to verify if a string is not starting and ending with ";" 我需要验证字符串是否不是以“;”开头和结尾 and if there is only one occurence of ";" 并且如果仅出现“;” if this special char is inside the string(not at the start , not at the end) . 如果此特殊字符位于字符串内(不在开头,不在结尾)。 I must use a regex in these case i think 我认为在这种情况下我必须使用正则表达式

For example : 例如 :

var regex =new RegExp("^\w+;\w+", "g"); ;

var test = ';azerty1;azerty2;azerty3'; //invalid
var test2 = 'azerty1;azerty2;azerty3'; //valid
var test3 = 'azerty1;azerty2;azerty3;'; //invalid
var test4 = 'azerty1;azerty2;;azerty3'; //invalid
var test5 = 'azerty1;azerty2;azerty3;azerty4'; //valid
var test6 = ';;azerty1;azerty2;azerty3;azerty4'; //invalid
var test7 = 'azerty1;azerty2;azerty3;azerty4;;'; //invalid
var test8 = 'azerty1azerty2azerty3azerty4'; //valid

var array = [test , test2 ,test3 ,test4 ,test5 ,test6 ,test7 , test8];

for(var i= 0; i < array.length; i++)
{
     if(regex.test(array[i]))
      {
        alert("TRUE");
      }
      else
      {
        alert("FALSE");
      }
} 

Any help would be appreciated Thank you very much 任何帮助将不胜感激非常感谢

As of es6 , we will benefit from String.includes , String.startsWith , String.endsWith and a lot of cool features natively, but until it is implemented and supported by all browsers you, can use this solution: es6开始 ,我们将受益于String.includesString.startsWithString.endsWith和许多很酷的本地功能,但是在所有浏览器都实现并支持它之前,您可以使用以下解决方案:

String.prototype.startsWith = function(str){
    var regex = new RegExp('^'+str);
    return  regex.test(this); 
}

String.prototype.endsWith = function(str){
    var regex = new RegExp(str+'$');
    return  regex.test(this); 
}

// String.contains in this case has a special behavior 
String.prototype.contains = function(str){    
    var tmp = this.split(str);
    return !this.startsWith(str) && tmp.length >= 2 && !this.endsWith(str);
}

var test = ';azerty1;azerty2;azerty3'; //invalid
var test2 = 'azerty1;azerty2;azerty3'; //valid
var test3 = 'azerty1;azerty2;azerty3;'; //invalid
var test4 = 'azerty1;azerty2;;azerty3'; //invalid
var test5 = 'azerty1;azerty2;azerty3;azerty4'; //valid
var test6 = ';;azerty1;azerty2;azerty3;azerty4'; //invalid
var test7 = 'azerty1;azerty2;azerty3;azerty4;;'; //invalid

var array = [test , test2 ,test3 ,test4 ,test5 ,test6 ,test7];

array.map(function(item){
    if(item.contains(';')){
        console.log('valid');
    }else{
        console.log('invalid');
    }
});

You need to put the reference inside the variable in order to check them. 您需要将引用放入变量中才能进行检查。 Also change the regex to /^\\w+(;\\w+)*$/ or in string format you need to escape \\ ( new RegExp("^\\\\w+(;\\\\w+)*$") ). 还要将正则表达式更改为/^\\w+(;\\w+)*$/或需要转义的字符串格式\\new RegExp("^\\\\w+(;\\\\w+)*$") )。 At last the modifier g have nothing to do with the regex since it's anchored with ^ and $ . 最后,修饰符g与正则表达式无关,因为它与^$锚定。

 // var regex = new RegExp("^\\\\w+(;\\\\w+)*$"); // there is no need to construct regex from string var regex = /^\\w+(;\\w+)*$/; var test = ';azerty1;azerty2;azerty3'; //invalid var test2 = 'azerty1;azerty2;azerty3'; //valid var test3 = 'azerty1;azerty2;azerty3;'; //invalid var test4 = 'azerty1;azerty2;;azerty3'; //invalid var test5 = 'azerty1;azerty2;azerty3;azerty4'; //valid var test6 = ';;azerty1;azerty2;azerty3;azerty4'; //invalid var test7 = 'azerty1;azerty2;azerty3;azerty4;;'; //invalid var array = [test, test2, test3, test4, test5, test6, test7]; for (var i = 0; i < array.length; i++) { console.log(array[i] + ' : ' + regex.test(array[i])); } 

You can use : /^((\\w+);)*\\w+$/gm 您可以使用: /^((\\w+);)*\\w+$/gm

 var regex = /^((\\w+);)*\\w+$/gm; ; var test = ';azerty1;azerty2;azerty3'; //invalid var test2 = 'azerty1;azerty2;azerty3'; //valid var test3 = 'azerty1;azerty2;azerty3;'; //invalid var test4 = 'azerty1;azerty2;;azerty3'; //invalid var test5 = 'azerty1;azerty2;azerty3;azerty4'; //valid var test6 = ';;azerty1;azerty2;azerty3;azerty4'; //invalid var test7 = 'azerty1;azerty2;azerty3;azerty4;;'; //invalid var array = [ test , test2 , test3 , test4 , test5 , test6 , test7 ]; for(var i= 0; i < array.length; i++) { if(regex.test(array[i])) { console.log("TRUE for " + array[i]); } else { console.log("FALSE for " + array[i]); } } 

Regarding 关于

I need to verify if a string is not starting and ending with ";" 我需要验证字符串是否不是以“;”开头和结尾。 and if there is only one occurence of ";" 并且如果仅出现“;” if this special char is inside the string(not at the start , not at the end) . 如果此特殊字符位于字符串内(不在开头,不在结尾)。

There are two issues: 有两个问题:

So, use 所以用

var regex = /^\w+;\w+$/;

This only matches a string starting with 1+ word chars, ; 这仅匹配以1+个单词char开头的字符串; , and ending with 1+ word chars. ,并以1个以上的字符char结束。

Also, avoid using a constructor notation when your regex pattern is known beforehand, use a regex literal notation (it is a general best practice). 另外,请避免在事先知道您的正则表达式模式时使用构造函数表示法,而应使用正则表达式文字表示法(这是一般的最佳做法)。

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

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