Replace comma

S

shapper

Hello,

I have an array which I am joining using levels.join(", ")

How can I replace the last comma on the array and replace it by the "
and "?

Thanks,
Miguel
 
E

Evertjan.

shapper wrote on 22 nov 2008 in comp.lang.javascript:
I have an array which I am joining using levels.join(", ")

How can I replace the last comma on the array and replace it by the "
and "?

You are not very clear, please show us a short example code.

If it is about a string containing multiple ", " and
ending with the same:

theString = theString.replace(/", "$/,'"and "');

===================

A better, imho, way is to strip the array of
a possible empty string member at the end before joining.

var temp = myarray.pop();
if (temp!='') myarray.push(temp);

Or if you are sure there always is one, just:

myarray.pop();
 
S

SAM

Le 11/22/08 4:30 PM, shapper a écrit :
Hello,

I have an array which I am joining using levels.join(", ")

levels.join(','); // without white space
or :
levels.join();
How can I replace the last comma on the array and replace it by the "
and "?

If there is a last coma it is because there is a last empty element.

You don't need to add the " "
level = levels.join(); // would has to be enough

or try :
level = '"'+levels.join()+'"';
 
S

shapper

shapper wrote on 22 nov 2008 in comp.lang.javascript:



You are not very clear, please show us a short example code.

If it is about a string containing multiple ", " and
ending with the same:

theString = theString.replace(/", "$/,'"and "');

===================

A better, imho, way is to strip the array of
a possible empty string member at the end before joining.

var temp = myarray.pop();
if (temp!='') myarray.push(temp);

Or if you are sure there always is one, just:

myarray.pop();

Hello,

I am using JQuery as follows:
$levels = $('input[name="Levels"]:checked + label');
levels = $levels.map(function() { return $(this).text(); }).get();
$theme.append(levels.join(", ")).append('<br />');

I added your suggestion:
$theme.append(levels.join(", ").replace(/", "$/,'"and "')).append
('<br />');

However it is not working. What am I doing wrong?

This is the code I have.

Thank You,
Miguel
 
E

Evertjan.

shapper wrote on 22 nov 2008 in comp.lang.javascript:
I am using JQuery as follows:

do not use JQuery.
$levels = $('input[name="Levels"]:checked + label');
levels = $levels.map(function() { return $(this).text(); }).get();
$theme.append(levels.join(", ")).append('<br />');

Do you understand what you are doing here?

I sure as hell I do not.
I added your suggestion:

Adding to a code you do not functionally understand
is hazardous to say the least.
$theme.append(levels.join(", ").replace(/", "$/,'"and "')).append
('<br />');
However it is not working. What am I doing wrong?

Now I can see what you ment,

levels.join(", ")

you did not add quotes to the join string,
which is not at all logical in a string join,
as there could be commas in the members,
but perhaps it is not a string join.

Try:

..replace(/, $/,'and ')

Better start debugging your code.
And perhaps also do not use regular expressions,
while you do not yet understand them.
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top