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

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -