0

Example text that I am dealing with. This text is a tooltip that is stored in a database that a user can edit using a WYSIWYG editor.

Example tooltip:

BlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlah
BlahBlahBlahBlah <a href="[SOME_COMPLETE URL]">BlahBlah</a>
BlahBlahBlahBlahBlahBlahBlahBlah

In short I need to replace that href to a javascript function in my app:

<a href="www.myurl.com/page/page/page">BlahBlah</a>

goes to:

<a href="javascript:void(0);" onclick="openOrFocus('www.myurl.com/page/page/page', 'windowName')">BlahBlah</a>

I'm trying to write a RegEx expression to find the href tag, get everything between the quotes. I then need to replace everything between the quotes using the above code and put the URL in the function call.

Here's what I've tried to far but it does not work (I struggle with Regular Expressions)

myTooltipText.replaceAll(".*href\\s*=\\s*\(.*)\"","href=\"javascript:void(0);\" onclick="openOrFocus('[SOME_COMPLETE_URL]', 'windowName')";

I'm using Java and Groovy.

Edit: The URL's are never the same; they change per tooltip as they go to an in depth context help.

2
  • The .* should be .*? to make them none-greedy Commented Dec 18, 2013 at 22:54
  • added the ? to both spots and it isn't matching anything. Commented Dec 18, 2013 at 23:09

1 Answer 1

1

You don't need .* before href and you're trying to escape your left parenthesis on your capturing group it looks like. You also missed escaping a quotation inside your replacement text, and left off your ending parenthesis ) at the end of your replaceAll function.

Try the following, this captures between your quotes and references to capture group $1 in the replacement.

myTooltipText.replaceAll("href\\s*=\\s*\"([^\"]*)\"", "href=\"javascript:void(0);\" onclick=\"openOrFocus('$1', 'windowName')\"");

See Working demo

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

1 Comment

Thank you so much, that worked perfectly! One of my issues was I wasn't setting the myTooltip variable to the replace all (noob mistake) and I needed to escape the dollar sign. Thank you again for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.