I want to check if a string match the following pattern
Having a text contains between Two "
and each text can be separated to another by a ;
the appending ;
is mantatory
I tried with the regular expression /"(.*?)";?/
but can't make it work
In the code snippet below test3
return true
but should not match the regex
I don't know if it's my regex or the way I try my regex
const regex = /"(.*?)";?/;
const test = `"hello"; "world";`;
const test2 = `"hello"; "world"`;
const test3 = `"hello; "world"`;
const test4 = `"hello"; world`;
const test5 = `"hello""world"`;
//should be true
console.log("test1",regex.test(test));
console.log("test2",regex.test(test2));
//should be false
console.log("test3",regex.test(test3));
console.log("test4",regex.test(test4));
console.log("test5",regex.test(test5));
/^"[^"]*"(?:\s*;\s*"[^"]*")+;?$/
---2---
---------3---------
-4
... can be broken down into ...
/^ ... $/
"[^"]*"
(?:\s*;\s*"[^"]*")+
;?
... which then translates to ...
( ... )
the following match but do not capture it (?: ... )
and force this pattern to be present at least once (?: ... )+
... within this group ...
(2)
.const regex = /^"[^"]*"(?:\s*;\s*"[^"]*")+;?$/;
const test1 = `"hello"; "world";`;
const test1a = `"hello" ; "world" ;"world"; "world"`;
const test2 = `"hello" ; "world"`;
const test2a = `"hello";"world" ; "world";"world"`;
const test3 = `"hello; "world"`;
const test4 = `"hello"; world`;
const test5 = `"hello""world"`;
const test6 = `"hello ; world;";"hello ;world"`;
const test6a = `"hello ;world;" "hello; world"`;
const test6b = `"hello; world"`;
//should be true
console.log("test1", regex.test(test1));
console.log("test1a", regex.test(test1a));
console.log("test2", regex.test(test2));
console.log("test2a", regex.test(test2a));
console.log("test6", regex.test(test6));
//should be false
console.log("test3", regex.test(test3));
console.log("test4", regex.test(test4));
console.log("test5", regex.test(test5));
console.log("test6a", regex.test(test6a));
console.log("test6b", regex.test(test6b));
.as-console-wrapper { min-height: 100%!important; top: 0; }