Reputation:
The conditions of the regex are as follows:
Starts with either digits or a '+' sign and ends with digits.
This is going to be used to validate a certain type of number. What I got so far is:
/^\d*|\+\d*$/
This regex seems to match any string though. How would a regex that matches my conditions look like?
The regex will be used in a JavaScript function.
Upvotes: 2
Views: 18362
Reputation: 174696
I think you want something like this,
^(?:[+\d].*\d|\d)$
^
Asserts that we are at the start.[+\d]
Matches a plus symbol or a digit..*
Matches any character zero or more times.\d
Matches a digit.|
OR\d
A single digit.$
Asserts that we are at the end.Use this if you want to match also a line which has a single plus or digit.
^[+\d](?:.*\d)?$
Upvotes: 15
Reputation: 785058
You need to use anchors ^
and $
on both sides of your regex and make first part +
or digit) optional.
You can use this regex:
^([+\d].*)?\d$
Upvotes: 6