I'm trying
String string = "123456";
if(string.startsWith("[0-9]") && string.endsWith("[0-9]")){
//code
}
And the if clause is never called.
The RegExp test() Method To check if a string contains only letters and numbers in JavaScript, call the test() method on this regex: /^[A-Za-z0-9]*$/ . If the string contains only letters and numbers, this method returns true . Otherwise, it returns false .
To check if a string ends with a number, call the test() method on a regular expression that matches one or more numbers at the end a string. The test method returns true if the regular expression is matched in the string and false otherwise.
I really recommend using the String. StartsWith method over the Regex. IsMatch if you only plan to check the beginning of a string.
The meta character “^” matches the beginning of a particular string i.e. it matches the first character of the string. For example, The expression “^\d” matches the string/line starting with a digit. The expression “^[a-z]” matches the string/line starting with a lower case alphabet.
Don't use a regex:
Character.isDigit(string.charAt(0)) &&
Character.isDigit(string.charAt(string.length()-1))
(see Character.isDigit()
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With