0

I have a code and i want to read only the name in the code. The name is inside quotes and parenthesis. I do not care about whole code line but the name only.

CODE

document.write('john smith') // i just want to read john smith using Regex in java.

Actually, i am directly reading through URL. Some of the data i need is in Javascript code and some in other tags and attributes. If i get the solution of this above, i hope, i can handle the rest myself.

jlordo
  • 37,490
  • 6
  • 58
  • 83
Rebbeca
  • 87
  • 12

1 Answers1

1

Perhaps something like this will work for you:

document\.write\(["']([^"']*)["']\)

Which will capture the name as the first capturing group. That is, when you execute the regex, you should be able to access it as index 1 of your match. (See this answer for an example using capturing groups).

Although, as a warning, all the advice against parsing HTML with regex goes double when you throw javascript and the like into the mix. If your actual application of this is remotely more complex than what you have here, you might want to consider another way to do this.

Community
  • 1
  • 1
femtoRgon
  • 32,893
  • 7
  • 60
  • 87