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

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -