String.replace() with regex

J

Jason C

I have this regex, which works as expected:

a = a.replace(/ itxtHarvested="(.*?)"/i, "");

I modified it to this:

a = a.replace(/\s*itxtHarvested="*(.*?)"*/i, "");

which I thought would be the same, but would make the /s and " optional. But, in practice, instead of just removing it, it was being converted to something like:

441?0?

(Or maybe 530"0"; I had made several modifications using the *, and now I don't remember which error went where.)

Does the * not mean "0 or more times" in Javascript like it does in other languages? I know that a few things are different with JS (like the /s modifier), so I wasn't sure.

Or, is it just something flawed in my logic that made it not work as expected?
 
T

Thomas 'PointedEars' Lahn

Jason said:
I have this regex, which works as expected:

a = a.replace(/ itxtHarvested="(.*?)"/i, "");

I modified it to this:

a = a.replace(/\s*itxtHarvested="*(.*?)"*/i, "");

which I thought would be the same, but would make the /s and " optional.

How can it be the same then? It would make leading white-space, and
optional in that it would allow 0 or more of them, including `"""""'.
(This is probably not what you want.)
But, in practice, instead of just removing it, it was being converted to
something like:

441?0?

(Or maybe 530"0"; I had made several modifications using the *, and now I
don't remember which error went where.)

No, it was not.
Does the * not mean "0 or more times" in Javascript like it does in other
languages?

There is no "Javascript": <http://PointedEars.de/es-matrix>

The `*' means the same in ECMAScript implementations as in other programming
languages.
I know that a few things are different with JS (like the /s
modifier), so I wasn't sure.

ECMAScript Regular Expressions do not natively support several PCRE
features, but you can emulate some. I have recently finished emulating
support for Unicode character property escape sequences, named subpatterns,
and the `x' modifier:

Or, is it just something flawed in my logic that made it not work as
expected?

Both your logic and your question are flawed.

<http://jibbering.com/faq/#posting>
<http://catb.org/~esr/faqs/smart-questions.html#beprecise>


PointedEars
 
H

Hans-Georg Michna

I have this regex, which works as expected:

a = a.replace(/ itxtHarvested="(.*?)"/i, "");

I modified it to this:

a = a.replace(/\s*itxtHarvested="*(.*?)"*/i, "");

The inner parentheses serve no purpose. I would remove them.

If, however, you are actually trying to match parentheses, then
you have to escape them: \( \)

Hans-Georg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top