String.replace() with regex

Discussion in 'Javascript' started by Jason C, Dec 17, 2011.

  1. Jason C

    Jason C Guest

    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?
     
    Jason C, Dec 17, 2011
    #1
    1. Advertisements

  2. 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.)
    No, it was not.
    There is no "Javascript": <http://PointedEars.de/es-matrix>

    The `*' means the same in ECMAScript implementations as in other programming
    languages.
    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:

    Both your logic and your question are flawed.

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


    PointedEars
     
    Thomas 'PointedEars' Lahn, Dec 17, 2011
    #2
    1. Advertisements

  3. 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
     
    Hans-Georg Michna, Dec 17, 2011
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.