Javascript jquery 替换不替换所有空格 -

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14120999/
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-24 15:46:46  来源:igfitidea点击:

jquery replace not replacing all spaces with -

javascriptjqueryreplace

提问by Norman

Why is my jquery not replacing all spaces with a '-'. It only replaces the first space with a '-'

为什么我的 jquery 没有用'-'. 它只用一个替换第一个空格'-'

$('.modhForm').submit(function(event) {

        var $this = $(this),
            action = $this.attr('action'),
            query = $this.find('.topsearchbar').val(); // Use val() instead of attr('value').

        if (action.length >= 2 && query.length >= 2 && query.lenght <=24) {

          // Use URI encoding
          var newAction = (action + '/' + query.replace(' ','-'));
          console.log('OK', newAction); // DEBUG

          // Change action attribute
          $this.attr('action', newAction);

        } else {
          console.log('To small to be any good'); // DEBUG

          // Do not submit the form
          event.preventDefault();
        }
    });

回答by Vishal Suthar

Try with this:

试试这个:

.replace(/\s/g,"-");

Demo: JSFiddle

演示:JSFiddle

回答by Michel Ayres

Try this:

尝试这个:

var str = 'a b c';
var replaced = str.split(' ').join('-');

回答by Jeffrey Alan SMith

It's: "if (action.length >= 2 && query.length >= 2 && query.length<=24) {"

它是:“if (action.length >= 2 && query.length >= 2 && query.length<=24) {”

Not: "if (action.length >= 2 && query.length >= 2 && query.lenght<=24) {"

不是:“if (action.length >= 2 && query.length >= 2 && query.lenght<=24) {”

回答by Zeeshan Mahboob

You can try with custom function

您可以尝试使用自定义功能

String.prototype.replaceAll = function (searchText, replacementText) {
    return this.split(searchText).join(replacementText);
};
var text = "This is Sample Text";
text.replaceAll(" ", "-");
//final output(This-is-Sample-Text)

回答by Denys Séguret

Use a regular expressionto replace all occurences :

使用正则表达式替换所有出现:

query.replace(/\ /g, '-')

回答by pala?н

Try this

尝试这个

query.replace(/ +(?= )/g,'-');

This still works, in case your query is undefiniedor NaN

这仍然有效,以防您的查询是undefiniedNaN

回答by asgoth

Replace all whitespace (including tabs, spaces, ...):

替换所有空格(包括制表符、空格等):

query.replace(/\s/g, '_');

回答by Mike Samuel

String.prototype.replaceonly replaces the first when its first argument is a string. To replace all occurrences you need to pass in a global regular expression as the first argument.

String.prototype.replace仅在第一个参数是字符串时替换第一个。要替换所有出现的内容,您需要传入一个全局正则表达式作为第一个参数。

replace

...

To perform a global search and replace, either include the g switch in the regular expression or if the first parameter is a string, include g in the flags parameter.

replace

...

要执行全局搜索和替换,请在正则表达式中包含 g 开关,或者如果第一个参数是字符串,则在 flags 参数中包含 g。

Others have shown a number of regular expressions that work for varying definitions of "space."

其他人展示了许多适用于不同“空间”定义的正则表达式。