-1

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

ulquiorra
  • 931
  • 4
  • 19
  • 39

4 Answers4

2

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');
    }
});
ismnoiet
  • 4,129
  • 24
  • 30
1

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]));
}
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
  • Why are you constructing a regexp from a string instead of using a regexp literal? Also, this accepts strings with multiple semi-colons in them, whereas the OP said *I need to verify...if there is only one occurrence of ";"*. –  Jun 22 '16 at 14:37
  • @torazaburo : it only accept string with single `;` separate.... like `azerty1;azerty2;azerty3;azerty4'` – Pranav C Balan Jun 22 '16 at 14:41
  • @torazaburo : can you give an example for "___Also, this accepts strings with multiple semi-colons in them___" – Pranav C Balan Jun 22 '16 at 14:42
  • @PranavCBalan Thank you , almost perfect but it fail if the string has not ; . My bad , i forgot this test case . Question updated . Thanks – ulquiorra Jun 22 '16 at 14:47
1

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]);
      }
}
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • Is this supposed to fail, or not, if there's no semi-colon? I read the OP's question as it's supposed to fail. Also, why are you passing a regexp literal to the RegExp constructor? –  Jun 22 '16 at 14:35
  • *regexp literal to the RegExp constructor* => My bad, I was lazy :) *Is this supposed to fail, or not, if there's no semi-colon?* => OP is unclear (IMHO), let's wait for clarification @torazaburo – Thomas Ayoub Jun 22 '16 at 14:38
  • @ThomasAyoub Thank you . Works perfectly too but i forgot indeed the case without semi colon my bad . Question updated . Thanks – ulquiorra Jun 22 '16 at 14:49
1

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

Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Well, since you need to allow multiple `;` inside the strings, you really need to use a group and set a quantifier to it. I'd use a non-capturing one: `/^\w+(?:;\w+)*$/`. Non-capturing groups `(?:...)` are only used to group, not to store captured values. – Wiktor Stribiżew Jun 22 '16 at 15:27