Connecting Tech Pros Worldwide Help | Site Map

regular expression question

 
LinkBack Thread Tools Search this Thread
  #1  
Old May 16th, 2007, 08:25 PM
laredotornado@zipmail.com
Guest
 
Posts: n/a
Default regular expression question

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


  #2  
Old May 16th, 2007, 09:15 PM
-Lost
Guest
 
Posts: n/a
Default Re: regular expression question

laredotornado@zipmail.com wrote:
Quote:
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.
  #3  
Old May 16th, 2007, 11:05 PM
Evertjan.
Guest
 
Posts: n/a
Default Re: regular expression question

laredotornado@zipmail.com wrote on 16 mei 2007 in comp.lang.javascript:
Quote:
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)
  #4  
Old May 17th, 2007, 02:35 AM
scripts.contact
Guest
 
Posts: n/a
Default Re: regular expression question

On May 16, 2:22 pm, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.comwrote:
Quote:
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))


  #5  
Old May 17th, 2007, 05:35 PM
Dr J R Stockton
Guest
 
Posts: n/a
Default Re: regular expression question

In comp.lang.javascript message <8P-dneqFHrL179bbnZ2dnUVZ_jadnZ2d@comcas
t.com>, Wed, 16 May 2007 17:07:51, -Lost <maventheextrawords@techie.com>
posted:
Quote:
>laredotornado@zipmail.com wrote:
Quote:
Quote:
> 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)".
Quote:
>I make no claims that this is the best possible solution. My Regular
>Expression knowledge is severely limited.
Then why respond?
Quote:
>One small change could be, since you know it is always (#):
Quote:
>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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
  #6  
Old May 17th, 2007, 05:35 PM
Randy Webb
Guest
 
Posts: n/a
Default Re: regular expression question

Dr J R Stockton said the following on 5/17/2007 11:40 AM:
Quote:
In comp.lang.javascript message <8P-dneqFHrL179bbnZ2dnUVZ_jadnZ2d@comcas
t.com>, Wed, 16 May 2007 17:07:51, -Lost <maventheextrawords@techie.com>
posted:
Quote:
>laredotornado@zipmail.com wrote:
>
Quote:
Quote:
>> 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)".
>
Quote:
>I make no claims that this is the best possible solution. My Regular
>Expression knowledge is severely limited.
>
Then why respond?
So that people like you can respond and try to needle them? Believe it
or not, a lot of people here post in the hopes of learning. Whether it
is by answering questions or by posting questions.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.