简体   繁体   中英

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:

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+)*$") ). At last the modifier g have nothing to do with the regex since it's anchored with ^ and $ .

 // 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

 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, ; , and ending with 1+ word chars.

Also, avoid using a constructor notation when your regex pattern is known beforehand, use a regex literal notation (it is a general best practice).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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