Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace all spaces in a string

I have two html text input, out of that what ever user type in first text box that need to reflect on second text box while reflecting it should replace all spaces to semicolon. I did to some extant and it replacing for first space not for all, I think I need to use .each function of Jquery, I have used .each function but I didn't get the result see this

HTML :

Title : <input type="text" id="title"><br/> Keyword : <input type="text" id="keyword"> 

Jquery:

$('#title').keyup(function() {     var replaceSpace = $(this).val();           var result = replaceSpace.replace(" ", ";");          $("#keyword").val(result);  }); 

Thanks.

like image 543
Rajesh Hatwar Avatar asked Nov 09 '13 06:11

Rajesh Hatwar


People also ask

How do you replace all spaces in a string %20?

A simple solution is to create an auxiliary string and copy characters one by one. Whenever space is encountered, place %20 in place of it. A better solution to do in-place assuming that we have extra space in the input string. We first count the number of spaces in the input string.

How do I remove all spaces from a string?

Use the String. replace() method to remove all whitespace from a string, e.g. str. replace(/\s/g, '') . The replace() method will remove all whitespace characters by replacing them with an empty string.

How do you replace all spaces in a string in Python?

The easiest approach to remove all spaces from a string is to use the Python string replace() method. The replace() method replaces the occurrences of the substring passed as first argument (in this case the space ” “) with the second argument (in this case an empty character “”).


1 Answers

var result = replaceSpace.replace(/ /g, ";"); 

Here, / /g is a regex (regular expression). The flag g means global. It causes all matches to be replaced.

like image 173
Paul Draper Avatar answered Oct 08 '22 09:10

Paul Draper