replace()
methodYou can simply use the JavaScript replace()
to replace the multiple spaces inside a string.
Let's take a look a
<script>
var myStr = 'The quick brown fox';
document.write('<p>' + myStr + '</p>'); // Browser doesn't display multiple spaces
var newStr = myStr.split(' ').join('-');
document.write('<p>' + newStr + '</p>'); // Print 'The--quick-brown-fox'
var myStr = myStr.replace(/ +/g, ' ');
document.write('<p>' + myStr + '</p>');
var newStr = myStr.split(' ').join('-');
document.write('<p>' + newStr + '</p>'); // Print 'The-quick-brown-fox'
</script>
t the following example to understand how it basically works:
<script>
var myStr = 'The quick brown fox';
alert(myStr); // Output 'The quick brown fox'
var newStr = myStr.replace(/ +/g, ' ');
alert(newStr); // Output 'The quick brown fox'
</script>
However, the difference is not visible if you print those strings on a web page, because browsers treat multiple spaces as single space unless you preserve white space.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to Generate QR Code in Laravel 8
QR(Quick Response) code is a type of bar...How to Make a Redirect in PHP
Use the PHP header() Function You can...How to check if an element is hidden using jQuery
Use the jQuery :hidden Selector The j...proc_open(): fork failed - Cannot allocate memory
Sometimes, when you run any composer com...Laravel 8 Google Recaptcha Tutorial Example
Google Recaptcha is Captcha service by G...