From: Brett on
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?



From: RobG on
On Aug 9, 1:46 am, Brett <bretttolb...(a)gmail.com> wrote:
> 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.

Use something that more closely approximates a real reference:

<h2>References</h2>
<ol>
<li><a name="Dewhurst"></a>Dewhurst, Stephen, C. <cite>&quot;C++
Common Knowledge: Essential Intermediate Programming&quot;/cite>
</ol>
<p>Here is a statement that references "&hellip;something writtten by
Dewhurst" <sup><a class="ref" href="#Dewhurst">[Dewhurst]</a></sup>


And do it all on the server - no javascript required.


--
Rob
From: Asen Bozhilov on
Brett wrote:

> Is there a way to convert a plain string into a regexp patter which
> matches the plain string?

/**
* Escape not allowed symbols in PatternCharacter
* PatternCharacter ::
* SourceCharacter but not any of:
* ^ $ \ . * + ? ( ) [ ] { } |
*/
function escapeRegExp(str) {
return str.replace(/[\^\$\\\.\*\+\?\(\)\[\]\{\}\|]/g, "\\$&");
}

escapeRegExp('[\\d]+'); //-> \[\\d\]\+
From: Asen Bozhilov on
Asen Bozhilov wrote:
> Brett wrote:
> > Is there a way to convert a plain string into a regexp patter which
> > matches the plain string?
>
> /**
>  * Escape not allowed symbols in PatternCharacter
>  * PatternCharacter ::
>  *    SourceCharacter but not any of:
>  * ^ $ \ . * + ? ( ) [ ] { } |
>  */
> function escapeRegExp(str) {
>     return str.replace(/[\^\$\\\.\*\+\?\(\)\[\]\{\}\|]/g, "\\$&");

More readable RegExp is:

/[$^\\.*+?()[\]{}|]/g

I think it is not a bad idea FAQ to add entry about this topic.

From: Ry Nohryb on
On Aug 8, 5:46 pm, Brett <bretttolb...(a)gmail.com> wrote:
> 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?

txt= 'some text plus [Dewhurst](Dewhurst, Stephen, C. "C++ Common
Knowledge: Essential Intermediate Programming") plus some more text
plus again [Dewhurst](Dewhurst, Stephen, C. "C++ Common Knowledge:
Essential Intermediate Programming") plus even more text';

linkMarkup = '[Dewhurst](Dewhurst, Stephen, C. "C++ Common Knowledge:
Essential Intermediate Programming")';

linkHtml = "<a href=\"javascript:alert('Dewhurst, Stephen, C. \"C++
Common Knowledge: Essential Intermediate Programming\"');\">Dewhurst</
a>";

while (txt.indexOf(linkMarkup) >= 0) txt= txt.replace(linkMarkup,
linkHtml);
--> "some text plus <a href="javascript:alert('Dewhurst, Stephen, C. "C
++ Common Knowledge: Essential Intermediate Programming"');">Dewhurst</
a> plus some more text plus again <a href="javascript:alert('Dewhurst,
Stephen, C. "C++ Common Knowledge: Essential Intermediate
Programming"');">Dewhurst</a> plus even more text"
--
Jorge.