Wednesday, 15 August 2012

In javascript how to add or remove 's' from a quantity string -



In javascript how to add or remove 's' from a quantity string -

i have string that's returned database such 7 months 5 days.

the issue when it's 1 month 1 day, it's still showing 1 months 1 days, notice letter s in months , days.

and i'm not allowed utilize brackets 1 month(s) 1 day(s).

please how can create status if it's less 2 days remove s?

the below not working. do?

js

var yymm = '1 months 1 days'; // simulated data. if (yymm.indexof('1') > -1) { yymm.replace('s', ''); console.log('yymm is: ', yymm); }

many thanks

single regexp replace can it:

class="snippet-code-js lang-js prettyprint-override">var reg = /\b1\b\s(month|day)s/g; alert( '1 months 1 days'.replace(reg, '1 $1') ); alert( '11 months 21 days'.replace(reg, '1 $1') ); alert( '12 months 1 days'.replace(reg, '1 $1') );

javascript string replace conditional-statements

No comments:

Post a Comment