Forum Moderators: open

Message Too Old, No Replies

Correct a link in a page via JS

without the ability to change any HTML

         

Trace

3:34 pm on Jun 16, 2008 (gmt 0)

10+ Year Member



My problem is, I have a system that outputs HTML. I have no control whatsoever on the HTML but I can add JS to it. The system does not parse "notes://" correctly and I need to fix it.

Here's the output;

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> New Document </title>
</head>
<body>

<table>
<tr>
<td>notes://<a href="http://www.example.com/">www.example.com/</a></td>
</tr>
</table>

<script type="text/javascript">
// script here to correct notes:// links
</script>

</body>
</html>

I need to fix the link so that it ends up like;
<a href="notes://www.example.com/">notes://www.example.com/</a>

Any ideas would be really appreciated.

Thanks!

Trace

6:30 pm on Jun 16, 2008 (gmt 0)

10+ Year Member



Using an online RegEx generator, I was able to find and replace the string in the editor.

So I have the RegEx now and it works in the online tool - I just cannot get it to work via JavaScript.

Help? Here's the RegEx

Match
notes://<a href[\\s]?=[\\s\"\']+(http:)(.*?)[\"\']+.*?>([^<]+¦.*?)?

Replace
<a href="notes:$2">notes://$3

Trace

7:21 pm on Jun 16, 2008 (gmt 0)

10+ Year Member



Welp - finally got it.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> New Document </title>
</head>
<body>

<table>
<tr>
<td>notes://<a href="http://www.cccc.com/c3245">www.cccc.com/c3245</a></td>
</tr>
</table>

<script type="text/javascript">
theString = document.body.innerHTML;

stringToFind = /notes:\/\/<a href[\\s]?=[\\s\"\']+(http:)(.*?)[\"\']+.*?>([^<]+¦.*?)?/i;
newString = theString.replace(stringToFind, '<a href=\"notes:$2\">notes:\/\/$3');

document.body.innerHTML = newString;
</script>

</body>
</html>

Hope it can help someone else at some point.