如何替换字符串中的所有空格

时间:2021-11-03 08:42:29

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 used that but i not got exact result see this

我有两个html文本输入,在第一个文本框中,需要对第二个文本框进行反射的任何用户类型都应该将所有空格替换为分号。我做了一些extant,它代替了第一个空格,我想我需要使用。Jquery的每个函数,我用过,但我没有得到确切的结果。

HTML :

HTML:

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

Jquery:

Jquery:

$('#title').keyup(function() {
    var replaceSpace = $(this).val(); 

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

        $("#keyword").val(result);

});

Thanks.

谢谢。

7 个解决方案

#1


69  

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

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

这里,/ /g是正则表达式。旗g表示全局。它导致所有匹配项都被替换。

#2


6  

Pure Javascript, without regular expression:

纯Javascript,没有正则表达式:

var result = 
  replaceSpacesText.
  split(" ").
  join("");

#3


2  

Simple code for replace all spaces

替换所有空格的简单代码

var str = 'How are you';
var replaced = str.split(' ').join('');

Out put: Howareyou

把:Howareyou

#4


1  

VERY EASY:

just use this to replace all white spaces with -:

用这个来替换所有的空格:

myString.replace(/ /g,"-")

#5


1  

    $('#title').keyup(function () {
        var replaceSpace = $(this).val();

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

        $("#keyword").val(result);

    });

Since the javascript replace function do not replace 'all', we can make use the regular expression for replacement. As per your need we have to replace all space ie the \s in your string globally. The g character after the regular expressions represents the global replacement. The seond parameter will be the replacement character ie the semicolon.

由于javascript替换函数不替换“all”,我们可以使用正则表达式进行替换。根据您的需要,我们必须在全球范围内替换您字符串中的所有空间。正则表达式后面的g字符表示全局替换。seond参数将是替换字符(即分号)。

#6


0  

takes care of multiple white spaces and replaces it for a single character

处理多个空格并替换为单个字符

myString.replace(/\s+/g, "-")

myString。替换(/ \ s + / g,“-”)

http://jsfiddle.net/aC5ZW/340/

http://jsfiddle.net/aC5ZW/340/

#7


0  

I came across this as well for me this has worked (covers most browsers):

我也遇到过这种情况,这种方法很有效(适用于大多数浏览器):

myString.replace(/[\s\uFEFF\xA0]/g, ';');

Inspired by this trim polyfill after hitting some bumps: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim#Polyfill

受此修饰多边形的启发,在撞到一些肿块后:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim# polyfill

#1


69  

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

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

这里,/ /g是正则表达式。旗g表示全局。它导致所有匹配项都被替换。

#2


6  

Pure Javascript, without regular expression:

纯Javascript,没有正则表达式:

var result = 
  replaceSpacesText.
  split(" ").
  join("");

#3


2  

Simple code for replace all spaces

替换所有空格的简单代码

var str = 'How are you';
var replaced = str.split(' ').join('');

Out put: Howareyou

把:Howareyou

#4


1  

VERY EASY:

just use this to replace all white spaces with -:

用这个来替换所有的空格:

myString.replace(/ /g,"-")

#5


1  

    $('#title').keyup(function () {
        var replaceSpace = $(this).val();

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

        $("#keyword").val(result);

    });

Since the javascript replace function do not replace 'all', we can make use the regular expression for replacement. As per your need we have to replace all space ie the \s in your string globally. The g character after the regular expressions represents the global replacement. The seond parameter will be the replacement character ie the semicolon.

由于javascript替换函数不替换“all”,我们可以使用正则表达式进行替换。根据您的需要,我们必须在全球范围内替换您字符串中的所有空间。正则表达式后面的g字符表示全局替换。seond参数将是替换字符(即分号)。

#6


0  

takes care of multiple white spaces and replaces it for a single character

处理多个空格并替换为单个字符

myString.replace(/\s+/g, "-")

myString。替换(/ \ s + / g,“-”)

http://jsfiddle.net/aC5ZW/340/

http://jsfiddle.net/aC5ZW/340/

#7


0  

I came across this as well for me this has worked (covers most browsers):

我也遇到过这种情况,这种方法很有效(适用于大多数浏览器):

myString.replace(/[\s\uFEFF\xA0]/g, ';');

Inspired by this trim polyfill after hitting some bumps: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim#Polyfill

受此修饰多边形的启发,在撞到一些肿块后:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim# polyfill