I've been trying to check if my string has an underscore or hyphen at either the beginning or the end.
// pass
sdq
lorem ipsum
abc_def
harry-_potter
// catch
fg_
_asq
_dqw-
-asq-sq
Tried out the following but I'm learning regex so I'm not quite sure how to go forward from here:
/^[-_]?.*[-_]$/
^ => beginning anchor
[-_]? => start with either - or _, optionally
.* => match any number of any character (I believe I could have used ranges here)
[-_]$ => check for - or _ at the end
I also know that one possible solution could be to use |
, which is used for conditions, and what I need is to be able to check that:
There is either a hypher or underscore at the beginning or end of the string.
How can I check this? Can I use something other than |
?