Requirements:
- There can be no capital letters in the string.
- The string cannot contain any of the characters '^$.?*+()'
- If '[' is present in the string it must be followed by zero or more characters other than '[' and ']', which must be followed by ']'. For example, [test] is valid, whereas [test not valid, test] and [[test]] are not valid. [test][test] is valid.
- '|' can be used if there is words after | like ||| not valid but |test valid word| not valid
function charPos(str, char) {
return str
.split("")
.map(function (c, i) { if (c == char) return i; })
.filter(function (v) { return v >= 0; });
}
function testString(urlPattern)
{
let regex = /[ $^*()+\[\]\\|.\/?]/g;
if(regex.test(urlPattern)){
let secondRegex = true;
let thirdRegex = true;
let regexData = /[ $^*()+\\\\.\/?]/g;
let regexDataNew = /[ $^*()+\\\.\/?]/g;
if(urlPattern.indexOf("[") < urlPattern.indexOf("]") && !regexData.test(urlPattern)){
secondRegex = false;
}
if(urlPattern.indexOf("[") == -1 && urlPattern.indexOf("]") == -1){
secondRegex = false;
}
if(urlPattern.indexOf("[[") != -1 || urlPattern.indexOf("]]") != -1){
secondRegex = true;
}
let pos = charPos(urlPattern,'|');
let largest = pos.sort((a,b)=>a-b)[pos.length - 1];
if(largest+1 < urlPattern.length && !regexDataNew.test(urlPattern)){
thirdRegex = false;
}
if(urlPattern.indexOf("|") == -1 ){
thirdRegex = false;
}
if(secondRegex || thirdRegex){
return 'Not Valid';
}
else {
return 'Valid';
}
}
else {
return 'Valid1';
}
}
// test case
testString('testttthhh@@|sss'); working
testString('testttthhh@@|'); working
testString('testttthhh@@|[]') working but need to show invalid.
If anyone have some solution or face same type problem help me to sort it out.
Thanks