-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

4
  • In the code as quoted array contains the strings 'test', 'test2', …: don't put quotes around variable names if you want to get the value of that variable.. Commented Jun 22, 2016 at 14:27
  • 4
    "..and if there is only one occurence of ";" .." I think that would make every test sting invalid. Commented Jun 22, 2016 at 14:28
  • @Richard . Yeah sorry my bad , i wrote this as hot code Commented Jun 22, 2016 at 14:34
  • @GolezTrol Sorry , I misstated what I meant :( Commented Jun 22, 2016 at 14:39

4 Answers 4

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');
    }
});
Sign up to request clarification or add additional context in comments.

Comments

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

4 Comments

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 ";".
@torazaburo : it only accept string with single ; separate.... like azerty1;azerty2;azerty3;azerty4'
@torazaburo : can you give an example for "Also, this accepts strings with multiple semi-colons in them"
@PranavCBalan Thank you , almost perfect but it fail if the string has not ; . My bad , i forgot this test case . Question updated . Thanks
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).

1 Comment

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

3 Comments

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?
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
@ThomasAyoub Thank you . Works perfectly too but i forgot indeed the case without semi colon my bad . Question updated . Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.