matching strings starting with (

Discussion in 'Javascript' started by Thomas 'PointedEars' Lahn, Nov 19, 2009.

  1. jc wrote:

    > The regex in this Javascript is not working to find strings that
    > start with "(". Escaping the ( and using chr(40) did not work either.


    There is no built-in chr().

    > Nor did double escaping it.


    It should work if you lose the chr().

    <http://jibbering.com/faq/#posting>
    <http://jibbering.com/faq/faq_notes/clj_posts.html#ps1DontWork>

    > I confirmed strings that match are being sent. And if I change my
    > match to another character like "-" it works.
    >
    > How do I do this?
    >
    >
    > if (strTitle.match("^"+"("))


    if (strTitle.match("^\\("))

    or

    if (strTitle.match(/^\(/))

    ,-[ECMAScript Language Specification, Edition 3 Final]
    |
    | 15.5.4.10 String.prototype.match (regexp)
    |
    | If regexp is not an object whose [[Class]] property is "RegExp",
    | it is replaced with the result of the expression new RegExp(regexp).
    | Let string denote the result of converting the this value to a string.
    | Then do one of the following:
    | [...]


    > Also, once past this, how do I remove (somechangingtext) from the
    > begging of a string?


    s = s.replace(new RegExp(expr));

    You will need to escape those characters in `expr' that should not have
    their default RegExp meaning, though.


    PointedEars
    --
    Danny Goodman's books are out of date and teach practices that are
    positively harmful for cross-browser scripting.
    -- Richard Cornford, cljs, <cife6q$253$1$> (2004)
    Thomas 'PointedEars' Lahn, Nov 19, 2009
    #1
    1. Advertising

  2. Thomas 'PointedEars' Lahn wrote:

    > jc wrote:
    >> Also, once past this, how do I remove (somechangingtext) from the
    >> begging of a string?

    >
    > s = s.replace(new RegExp(expr));


    s = s.replace(new RegExp(expr), replacement);

    > You will need to escape those characters in `expr' that should not have
    > their default RegExp meaning, though.


    (RTFM)


    PointedEars
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
    Thomas 'PointedEars' Lahn, Nov 19, 2009
    #2
    1. Advertising

  3. Thomas 'PointedEars' Lahn

    jc Guest

    The regex in this Javascript is not working to find strings that
    start with "(". Escaping the ( and using chr(40) did not work either.
    Nor did double escaping it.

    I confirmed strings that match are being sent. And if I change my
    match to another character like "-" it works.

    How do I do this?


    if (strTitle.match("^"+"("))
    {
    alert("found");



    }


    Also, once past this, how do I remove (somechangingtext) from the
    begging of a string?


    Thanks.
    jc, Nov 19, 2009
    #3
  4. jc <> writes:

    > The regex in this Javascript is not working to find strings that
    > start with "(". Escaping the ( and using chr(40) did not work either.
    > Nor did double escaping it.
    >
    > I confirmed strings that match are being sent. And if I change my
    > match to another character like "-" it works.
    >
    > How do I do this?
    >
    >
    > if (strTitle.match("^"+"("))
    > {
    > alert("found");
    > }


    Thomas has already showed you how to do this correctly with a RegExp.
    For so simple a test, I'd prefer just doing it directly:
    if (strTitle.charCodeAt(0) == 0x28) {
    alert("found");
    }

    > Also, once past this, how do I remove (somechangingtext) from the
    > begging of a string?


    This might do it (remove somechangingtext from the beginning of a
    string):
    string = string.substring(somechangingtext.length);
    It does assume that the string actually starts with the changing text.

    /L
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
    Lasse Reichstein Nielsen, Nov 19, 2009
    #4
  5. Lasse Reichstein Nielsen wrote:

    > For so simple a test, I'd prefer just doing it directly:
    > if (strTitle.charCodeAt(0) == 0x28) {
    > alert("found");
    > }


    Directly (and slightly more compatible) would be

    if (strTitle.charAt(0) === "(")

    It might even be more efficient as the character value does not need to be
    determined externally, no matter the more efficient strict comparison.


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
    Thomas 'PointedEars' Lahn, Nov 19, 2009
    #5
  6. Thomas 'PointedEars' Lahn <> writes:

    > Lasse Reichstein Nielsen wrote:
    >
    >> For so simple a test, I'd prefer just doing it directly:
    >> if (strTitle.charCodeAt(0) == 0x28) {
    >> alert("found");
    >> }

    >
    > Directly (and slightly more compatible) would be
    >
    > if (strTitle.charAt(0) === "(")


    I doubt there are any browsers still alive that doesn't support charCodeAt
    (but it's true that it didn't always exist).

    My thought was rather that the charCodeAt doesn't need to
    (potentially) create a new string object and do string comparison, but
    can just return and compare with a simple integer.

    However, it's not clearly better if you look at different browsers:

    Firefox 3.5: charAt is ~33% faster.
    Opera 10: charCodeAt is ~5% faster.
    IE8(x64): charCodeAt is ~75% faster.
    Chrome 4: charCodeAt is ~110% faster.
    Safari 4: charCodeAt is ~85% faster.

    This was measured using:

    (function() {
    var c = "(oglabogla)";
    var N = 1000000; // Vary number for slower browsers.
    var t0 = new Date();
    for (var i = 0; i < N; i++) {
    var eq1 = c.charAt(0) === "(";
    }
    var t1 = new Date();
    for (var i = 0; i < N; i++) {
    var eq1 = c.charCodeAt(0) === 0x28;
    }
    var t2 = new Date();
    var d = (t1-t0)/(t2-t1); // speed of charCodeAt relative to charAt
    return d;
    })()

    Firefox clearly stands out. I haven't checked 3.6b3.

    /L
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
    Lasse Reichstein Nielsen, Nov 19, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    756
    Malcolm
    Jun 24, 2006
  2. anonym
    Replies:
    1
    Views:
    1,015
    Knute Johnson
    Jan 15, 2009
  3. Karin Lagesen

    matching strings in a large set of strings

    Karin Lagesen, Apr 29, 2010, in forum: Python
    Replies:
    13
    Views:
    456
    Bryan
    May 3, 2010
  4. Helmut Jarausch
    Replies:
    3
    Views:
    326
    Dave Angel
    Apr 30, 2010
  5. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    233
    Marc Bissonnette
    Jan 13, 2004
Loading...

Share This Page