admin管理员组

文章数量:1026073

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
Add a ment  | 

1 Answer 1

Reset to default 3

Have 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();
}

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
Add a ment  | 

1 Answer 1

Reset to default 3

Have 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();
}

本文标签: javascriptusing REGEX on textInput in react nativeStack Overflow