regular expression question

Discussion in 'Javascript' started by laredotornado@zipmail.com, May 16, 2007.

  1. Guest

    Hi,

    I have a span that contains text of the form

    var spanHtml = "My Tab Content (7)";

    The content is guaranteed to end with a string of the form "(#)" where
    "#" is a whole number. My question is, how would I write a regular
    expression that would change the value of #, given another number?
    For example, given the above variable, if I had

    var newNumber = 15;

    I would want the variable spanHtml to contain

    "My Tab Content (15)".

    Thanks, - Dave
    , May 16, 2007
    #1
    1. Advertising

  2. -Lost Guest

    wrote:
    > Hi,
    >
    > I have a span that contains text of the form
    >
    > var spanHtml = "My Tab Content (7)";
    >
    > The content is guaranteed to end with a string of the form "(#)" where
    > "#" is a whole number. My question is, how would I write a regular
    > expression that would change the value of #, given another number?
    > For example, given the above variable, if I had
    >
    > var newNumber = 15;
    >
    > I would want the variable spanHtml to contain
    >
    > "My Tab Content (15)".


    var str1 = 'My Tab Content (7)';
    var _regexp = /[0-9]+/;
    alert(str1.replace(_regexp, '15'));

    I make no claims that this is the best possible solution. My Regular
    Expression knowledge is severely limited.

    One small change could be, since you know it is always (#):

    var _regexp = /\([0-9]+\)/;
    alert(str1.replace(_regexp, '(15)');

    Again though, I am sure a regexp guru will along momentarily to suggest
    an improvement.

    --
    -Lost
    Remove the extra words to reply by e-mail. Don't e-mail me. I am
    kidding. No I am not.
    -Lost, May 16, 2007
    #2
    1. Advertising

  3. Evertjan. Guest

    wrote on 16 mei 2007 in comp.lang.javascript:

    > Hi,
    >
    > I have a span that contains text of the form
    >
    > var spanHtml = "My Tab Content (7)";
    >
    > The content is guaranteed to end with a string of the form "(#)" where
    > "#" is a whole number. My question is, how would I write a regular
    > expression that would change the value of #, given another number?
    > For example, given the above variable, if I had
    >
    > var newNumber = 15;
    >
    > I would want the variable spanHtml to contain
    >
    > "My Tab Content (15)".


    function doit(s,n) {
    return s.replace(/\(\d+\)$/,'('+n+')')
    }

    alert(doit("My Tab Content (7)",15))
    alert(doit("Blah Blah (12345)",128))


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., May 16, 2007
    #3
  4. On May 16, 2:22 pm, ""
    <> wrote:
    > Hi,
    >
    > I have a span that contains text of the form
    >
    > var spanHtml = "My Tab Content (7)";
    >
    > The content is guaranteed to end with a string of the form "(#)" where
    > "#" is a whole number. My question is, how would I write a regular
    > expression that would change the value of #, given another number?
    > For example, given the above variable, if I had
    >


    var content="My Tab Content (7)"
    var regexp=/\d+(?=\)$)/
    content=content.replace(regexp,8)

    or

    var content="My Tab Content (7)"
    content=content.slice(0,-2)+8+content.slice(-1))
    scripts.contact, May 17, 2007
    #4
  5. In comp.lang.javascript message <8P-dneqFHrL179bbnZ2dnUVZ_jadnZ2d@comcas
    t.com>, Wed, 16 May 2007 17:07:51, -Lost <>
    posted:
    > wrote:


    >> I have a span that contains text of the form
    >> var spanHtml = "My Tab Content (7)";
    >> The content is guaranteed to end with a string of the form "(#)"
    >>where
    >> "#" is a whole number. My question is, how would I write a regular
    >> expression that would change the value of #, given another number?
    >> For example, given the above variable, if I had
    >> var newNumber = 15;
    >> I would want the variable spanHtml to contain
    >> "My Tab Content (15)".


    >I make no claims that this is the best possible solution. My Regular
    >Expression knowledge is severely limited.


    Then why respond?

    >One small change could be, since you know it is always (#):


    >Again though, I am sure a regexp guru will along momentarily to suggest
    >an improvement.


    Then why respond so soon?

    spanHtml = spanHtml.replace(/\d/, 15) ;

    If the input may contain multiple digits, change \d to \d+ .
    If the input may be signed, precede /d by [+-]? .

    It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr J R Stockton, May 17, 2007
    #5
    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. Andrew Munn

    Regular expression question...

    Andrew Munn, Jun 29, 2003, in forum: Perl
    Replies:
    1
    Views:
    2,097
    rakesh sharma
    Jun 30, 2003
  2. Glenn Kidd

    Regular expression question

    Glenn Kidd, Aug 18, 2003, in forum: Perl
    Replies:
    0
    Views:
    915
    Glenn Kidd
    Aug 18, 2003
  3. VSK
    Replies:
    2
    Views:
    2,261
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    822
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    11,909
    vforvikash
    Dec 29, 2008
Loading...

Share This Page