Home > front end >  Regular expression to allow only one special character Middle on the string
Regular expression to allow only one special character Middle on the string

Time:08-06

Need a regex to match below text

testword
testWord
TestA/Test
testt test
TestA-Test

I want to check whether in a given string two texts are joined with a space dash or slash string should start with a test and end with a text These are the possible word

"wordOne/wordTwo"
"wordOne-wordTwo"
"wordOne wordTwo"
"wordOne"

Tried with this. didn't work

^[a-zA-Z][/- ]?[a-zA-Z]

CodePudding user response:

It is a bit hard to understand what you are looking for but it looks like what you want is something more like ^[a-zA-Z] [\/\- ]?[a-zA-Z]

You are missing is after the [a-zA-Z] to indicate there is one or more, and then you need \ infront of - and / to escape

  • Related