[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: string.replace not working - D'OH!



On Feb 17, 2008 11:38 AM, I wrote:

> At this URL (which happens to be Brown v. Bd. of Ed.),
> http://bulk.resource.org/courts.gov/c/US/347/347.US.483.html, I'm
> trying to convert data which looks so, " 123 U.S. 456", into a link,
> so, "<a href=\"../123/123.US.456.html\"> 123 U.S. 456</a&gt".
>
> This text.replace works on a literal string: "blah blah 123 U.S. 456
> blah blah 789 U.S. 123".replace(/\s+(\d+)\s+U\.S\.\s+(\d+)/g, " <a
> href=\"../$1/$1.US.$2.html\">$&</a>"); replacing both
> citations with the appropriate link.
>
> but I cannot get it to work on the document.  Here's the script:
>
> var b = document.body.innerHTML;
> b.replace(/\s+(\d+)\s+U\.S\.\s+(\d+)/g, " <a
> href=\"../$1/$1.US.$2.html\">$&</a>");
> b = "<!--modified content-->" + b;
> document.body.innerHTML = b;

I realized that the string.replace function doesn't actually do
anything to the string, so I have to assign b.replace to a variable
and write that back, something like 'var c = b.replace...', then
document.body.innerHTML = c.  That, though, writes the string back
literally into the page, even when I replace &lt; with <, etc.

So, I'm going to do a document.evaluate to get all the text nodes I
want, and then process them, replacing any citations to US reports
with a document.createElement('a').

Sorry to be so thick today!

pw


Reply to: