How can I create a regex for that returns true if it has only numbers and '+' basically 0-9 & +. Using javascript or jQuery.
Asked
Active
Viewed 40 times
-4
-
What is your understanding of regexes and using them in JS, and what have you tried? – Harris Feb 07 '17 at 19:37
1 Answers
2
- Regex for plus anywhere:
/^[0-9+]+$/ - Regex for plus only infront:
/^\+?[0-9]+$/
What it does:
^Matches the beginning of the string[0-9+]Matches0123456789++Matches one or more$Matches the end of the string
Other version:
\+?Matches zero or one plus signs in the front
Maybe try regexr for future regex development.
How to test in code:
function isOnlyNumber(str) {
return /^[0-9+]+$/.test(str);
}
Le 'nton
- 366
- 3
- 22