I can't seem to make my regex work so i think I must be doing something wrong. If anyone could help me out that would be great.
here is my regex function
let validatePlate = (plate) => {
var re = /(^[A-Z]{2}[0-9]{2}\s?[A-Z]{3}$)|(^[A-Z][0-9]{1,3}[A-Z]{3}$)|(^[A-Z]{3}[0-9]{1,3}[A-Z]$)|(^[0-9]{1,4}[A-Z]{1,2}$)|(^[0-9]{1,3}[A-Z]{1,3}$)|(^[A-Z]{1,2}[0-9]{1,4}$)|(^[A-Z]{1,3}[0-9]{1,3}$)|(^[A-Z]{1,3}[0-9]{1,4}$)|(^[0-9]{3}[DX]{1}[0-9]{3}$)/
return re.test(plate);
};
and I use it in textInput as follows:
<TextInput
onChange={validatePlate}
editable={true}
}}>
Hello
</TextInput>
It still allows special characters, etc.
FYI: The regex is for filtering UK numberplates.
I can't seem to make my regex work so i think I must be doing something wrong. If anyone could help me out that would be great.
here is my regex function
let validatePlate = (plate) => {
var re = /(^[A-Z]{2}[0-9]{2}\s?[A-Z]{3}$)|(^[A-Z][0-9]{1,3}[A-Z]{3}$)|(^[A-Z]{3}[0-9]{1,3}[A-Z]$)|(^[0-9]{1,4}[A-Z]{1,2}$)|(^[0-9]{1,3}[A-Z]{1,3}$)|(^[A-Z]{1,2}[0-9]{1,4}$)|(^[A-Z]{1,3}[0-9]{1,3}$)|(^[A-Z]{1,3}[0-9]{1,4}$)|(^[0-9]{3}[DX]{1}[0-9]{3}$)/
return re.test(plate);
};
and I use it in textInput as follows:
<TextInput
onChange={validatePlate}
editable={true}
}}>
Hello
</TextInput>
It still allows special characters, etc.
FYI: The regex is for filtering UK numberplates.
Share Improve this question asked Feb 12, 2020 at 16:25 UnluckyLADUnluckyLAD 1893 silver badges21 bronze badges 1- 1 You are not returning any string, you are returning the validation result, true or false – Ian Vasco Commented Feb 12, 2020 at 16:30
1 Answer
Reset to default 3Have you tried wrapping your regexp in new RegExp()
For example:
const nameRegex = new RegExp(/^[a-z ,.'-]+(\s)([a-z ,.'-])+$/i);
if (nameRegex.test(inputValue)) {
doSomething();
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745632878a4637214.html
评论列表(0条)