Prev: MBT shoes will shape up your body everyday
Next: FAQ Topic - My element is named myselect[], how do I access it? (2010-08-09)
From: Brett on 8 Aug 2010 11:46 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 8 Aug 2010 23:04 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>"C++ Common Knowledge: Essential Intermediate Programming"/cite> </ol> <p>Here is a statement that references "…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 9 Aug 2010 08:15 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 9 Aug 2010 08:25 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 9 Aug 2010 08:49
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. |