I can use
var regex = /[$&+,:;=?@#|]/;
if(elem.match(regex)) { // do something
}
to find whether there is any special characters in string in Javascript.
How can I use the similar regular expression in Java?
I have tried:
str.match("\\="); // return false
For example:
String str = "123=456";
I tried to detect "=" in str.
That did not work. What am I doing wrong?
Try this.
Pattern regex = Pattern.compile("[$&+,:;=?@#|]");
Matcher matcher = regex.matcher("123=456");
if (matcher.find()){
// Do something
}
EDIT: matches()
checks all the string and find()
finds it in any part of the string.
A link: http://docs.oracle.com/javase/tutorial/essential/regex/index.html
Use String.matches(). Read the Javadocs for supported syntax.
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