From: SAM on
Le 08/08/10 17:46, Brett a �crit :
> 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")'

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

or maybe :

linkMarkup = /\[Dewhurst]\(Dewhurst, Stephen, C. \"C\+\+ Common
Knowledge:[\n\r]*Essential Intermediate Programming\")/;

(both in one line, and linkHtml too)

into :

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


> 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.

I think that is only possible with a "real" regexp (that will search all
characters between 2 tags (or marker) )

linkMarkup = /\[Dewhurst][^_]*\[\/Dewhurst]/;

> 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.

txt.replace(/\[Dewhurst][^_]*\[\/Dewhurst]/g, linkHtml);


I think that is not the + the problem
I think it's the line return that causes troubles
and, perhaps too, the " and ' and ( in replacing string


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

the "plain" string must be first a "string" (in JS understanding)

--
sm
From: Ry Nohryb on
On Aug 9, 3:42 pm, SAM <stephanemoriaux.NoAd...(a)wanadoo.fr.invalid>
wrote:
> Le 08/08/10 17:46, Brett a écrit :
>
>
>
>
>
>
>
>
>
> > 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")'
>
> linkMarkup = '[Dewhurst](Dewhurst, Stephen, C. \"C++ Common
> Knowledge:\nEssential Intermediate Programming\")';
>
> or maybe :
>
> linkMarkup = /\[Dewhurst]\(Dewhurst, Stephen, C. \"C\+\+ Common
> Knowledge:[\n\r]*Essential Intermediate Programming\")/;
>
> (both in one line, and linkHtml too)
>
> into :
>
> linkHtml = '<a href="javascript:alert(\'(Dewhurst, Stephen, C. \\"C++
> Common Knowledge: Essential Intermediate Programming\\")\')">Dewhurst</a>';
>
> > 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.
>
> I think that is only possible with a "real" regexp (that will search all
> characters between 2 tags (or marker) )
>
> linkMarkup = /\[Dewhurst][^_]*\[\/Dewhurst]/;
>
> > 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.
>
> txt.replace(/\[Dewhurst][^_]*\[\/Dewhurst]/g, linkHtml);
>
> I think that is not the + the problem
> I think it's the line return that causes troubles
> and, perhaps too, the " and ' and ( in replacing string
>
> > Is there a way to convert a plain string into a regexp patter which
> > matches the plain string?
>
> the "plain" string must be first a "string" (in JS understanding)

But I wonder, why the hassle when you can do it by looping an ordinary
replace ? Is it because regexps are sooo cool that one should use them
amap even when/if they're not the right/more convenient tool for the
task at hand ?
:-)
--
Jorge.
From: SAM on
Le 09/08/10 14:49, Ry Nohryb a �crit :
> 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';

And what about (what I think OP wanted) :

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

???

--
sm
From: SAM on
Le 09/08/10 15:55, Ry Nohryb a �crit :
> But I wonder, why the hassle when you can do it by looping an ordinary
> replace ?

You're talking about a text-editor ?
Yes a text-editor by copy/past can search a multi-lines text
(and then replace it)

> Is it because regexps are sooo cool that one should use them
> amap even when/if they're not the right/more convenient tool for the
> task at hand ?

It's not the fault to RegExp if JS breaks on a line return
(text-editor's line return in a JS string)

Even in my text-editor I use RegExp for multi-replacements,
it's really too cool ;-)

search :
art: (\d+)
replace all :
article: \1 - ref: shop-\1

In JS :
texto.replace(/art: (\d+)/g,'article: $1 - ref: shop-$1');


--
sm
From: Ry Nohryb on
On Aug 9, 4:34 pm, SAM <stephanemoriaux.NoAd...(a)wanadoo.fr.invalid>
wrote:
> Le 09/08/10 15:55, Ry Nohryb a écrit :
>
> > But I wonder, why the hassle when you can do it by looping an ordinary
> > replace ?
>
> You're talking about a text-editor ?
> Yes a text-editor by copy/past can search a multi-lines text
> (and then replace it)
>
> > Is it because regexps are sooo cool that one should use them
> > amap even when/if they're not the right/more convenient tool for the
> > task at hand ?
>
> It's not the fault to RegExp if JS breaks on a line return
> (text-editor's line return in a JS string)
>
> Even in my text-editor I use RegExp for multi-replacements,
> it's really too cool ;-)
>
> search :
>         art: (\d+)
> replace all :
>         article: \1 - ref: shop-\1
>
> In JS :
>         texto.replace(/art: (\d+)/g,'article: $1 - ref: shop-$1');

But in this case, the OP has a string that must be escaped if it's to
be used as a regexp for the search, therefore, I'd say, well, then
don't use it as a regexp, just loop using a regular search (not a //g
regexp) until done. BTW that's because I'm guessing that when he says
multiline he really means he wants to replace multiple instances, that
is, a //g regexp, which is no more than ~ a simple loop.
--
Jorge.