ulquiorra
ulquiorra

Reputation: 945

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

Upvotes: -1

Views: 138

Answers (4)

ismnoiet
ismnoiet

Reputation: 4169

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');
    }
});

Upvotes: 2

Pranav C Balan
Pranav C Balan

Reputation: 115272

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]));
}

Upvotes: 1

Thomas Ayoub
Thomas Ayoub

Reputation: 29471

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]);
      }
}

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627507

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).

Upvotes: 1

Related Questions