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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19873002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:44:37  来源:igfitidea点击:

How to replace all spaces in a string

javascriptjquerystringreplace

提问by Rajesh Hatwar

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 文本输入,其中用户在第一个文本框中键入的内容需要反映在第二个文本框中,同时反映它应该将所有空格替换为分号。我做了一些现存的并且它不是全部替换第一个空格,我想我需要使用 Jquery 的 .each 函数,我已经使用了 .each 函数但我没有得到结果 看到这个

HTML :

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.

谢谢。

回答by Paul Draper

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

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

这里,/ /g是一个正则表达式(正则表达式)。国旗g意味着全球。它会导致所有匹配项被替换。

回答by Zon

Pure Javascript, without regular expression:

纯 Javascript,没有正则表达式:

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

回答by CommonSenseCode

VERY EASY:

好简单:

just use this to replace all white spaces with -:

只是用它来取代所有空格-

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

回答by ricks

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

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

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

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

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

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

回答by Arshid KV

Simple code for replace all spaces

替换所有空格的简单代码

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

Out put:Howareyou

输出:你好

回答by Jobelle

    $('#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',我们可以使用正则表达式进行替换。根据您的需要,我们必须全局替换所有空格,即字符串中的 \s。正则表达式后面的 g 字符表示全局替换。第二个参数将是替换字符,即分号。

回答by stefan

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

在遇到一些颠簸后受到这个修剪 polyfill 的启发:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim#Polyfill

回答by Pushkar

You can do the following fix for removing Whitespaces with trim and @symbol:

您可以执行以下修复以删除带有修剪和@符号的空格:

var result = string.replace(/ /g, '');  // Remove whitespaces with trimmed value
var result = string.replace(/ /g, '@'); // Remove whitespaces with *@* symbol