regular expression question

L

laredotornado

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
 
L

-Lost

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.
 
E

Evertjan.

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))
 
S

scripts.contact

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))
 
D

Dr J R Stockton

In comp.lang.javascript message <8P-dneqFHrL179bbnZ2dnUVZ_jadnZ2d@comcas
(e-mail address removed) wrote:
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.
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top