javascript - How to include whitespaces in string replace? -


i need replace strings '5 hours' '5h'. i'm able '5 h'. how remove space in between? when replace "hours" in function " hours" whitespace, replaces nothing , returns '5 hours'.

$('div.block_name span').text(function(){     return $(this).text().replace("hours","h"); }); 

you can use regex so:

// "\s?" means find space if exists  console.log("5 hours".replace(/\s?hours/, 'h')); // 5h  console.log("5hours".replace(/\s?hours/, 'h')); // 5h


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -