B
Brett
I'm working on a project where a paragraph of text may contain markup
such as:
[Dewhurst](Dewhurst, Stephen, C. "C++ Common Knowledge: Essential
Intermediate Programming")
I want to replace any instance of the above markup with an HTML link.
E.g. the link text is "Dewhurst" and clicking it produces an alert
with the full citation.
I've already written code to find each markupLink and convert it to
the desired HTML. The problem I have is putting it back into the
paragraph.
Suppose I've converted
linkMarkup = '[Dewhurst](Dewhurst, Stephen, C. "C++ Common Knowledge:
Essential Intermediate Programming")'
into
linkHtml = "<a href=\"javascript:alert('Dewhurst, Stephen, C. \"C++
Common Knowledge: Essential Intermediate Programming\"');\">Dewhurst</
a>"
I want to do a multi-line replace, replacing linkMarkup with
linkHtml.
txt.replace(new RegExp(linkMarkup,'m'), linkHtml) doesn't work because
linkMarkup isn't a regexp pattern, it's just a string. Characters such
as the '++' in C++ need to be escaped.
Is there a way to convert a plain string into a regexp patter which
matches the plain string?
such as:
[Dewhurst](Dewhurst, Stephen, C. "C++ Common Knowledge: Essential
Intermediate Programming")
I want to replace any instance of the above markup with an HTML link.
E.g. the link text is "Dewhurst" and clicking it produces an alert
with the full citation.
I've already written code to find each markupLink and convert it to
the desired HTML. The problem I have is putting it back into the
paragraph.
Suppose I've converted
linkMarkup = '[Dewhurst](Dewhurst, Stephen, C. "C++ Common Knowledge:
Essential Intermediate Programming")'
into
linkHtml = "<a href=\"javascript:alert('Dewhurst, Stephen, C. \"C++
Common Knowledge: Essential Intermediate Programming\"');\">Dewhurst</
a>"
I want to do a multi-line replace, replacing linkMarkup with
linkHtml.
txt.replace(new RegExp(linkMarkup,'m'), linkHtml) doesn't work because
linkMarkup isn't a regexp pattern, it's just a string. Characters such
as the '++' in C++ need to be escaped.
Is there a way to convert a plain string into a regexp patter which
matches the plain string?