0

I am trying to convert JavaScript regular expression to java regular expression. here is my current javascript regular expression that I am trying to convert :

var re = /\a(?![^<]*>)/g;

I have searched and found out that I need to change the back-slash / into double back-slashes // and start with " but what about /g at the end? do we need to change it too? Thank you

5
  • could you please why the down vote? please advice. thank you Commented Apr 14, 2017 at 7:02
  • not sure, /g is not recognized stackoverflow.com/questions/4788413/… maybe also see stackoverflow.com/questions/4628715/… Commented Apr 14, 2017 at 9:33
  • The downvote is probably because your question is poorly written and poorly researched. (And it probably means that people think your question wouldn't be useful for others in the future. Not least because the writing is unclear ... and (based on the comments on the answer) the regex is not correct in Javascript either. Commented Apr 14, 2017 at 10:39
  • @StephenC the regex is correct in Javascript it is working fine. Also I edited my question to make it more clear. Thank you for your comment Commented Apr 14, 2017 at 19:30
  • So what does the "\a" mean in the Javascript version?? They have pretty much concluded that it is nonsense in the Java version. Commented Apr 15, 2017 at 0:09

1 Answer 1

2

The equivalent regex is:

string regex = "\\a(?![^<]*>)";

There is no equivalent of the g flag in JAVA, you have to use replaceall() instead of replace() if you want to replace or use a matcher and a while loop (see How can I find all matches to a regular expression in android).

Sign up to request clarification or add additional context in comments.

6 Comments

What is the purpose of escaping the letter a in a regex? In JS, /\a/.exec("\\a")[0] => "a". Java regex engine does not treat \a in any special way either.
@WiktorStribiżew absolutely no use, except to show OP that a backslash should be escaped in a string
I used that but it is not working ..... it should detect any letter a outside <a /> tags
@MuadhProgrammer could you provide input
@MuadhProgrammer in Java, the \a is interpreted as the ASCII 7 char, remove the backslashes and it should work. See demo
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.