如何替换字符串中的所有空格 [英] How to replace all spaces in a string

查看:565
本文介绍了如何替换字符串中的所有空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个html文本输入,用户在第一个文本框中键入的内容都需要在第二个文本框上反映的同时应将其替换为分号.我做了一些现存的操作,并且它不是全部代替第一个空格,我想我需要使用Jquery的.each函数,我使用了.each函数,但没有得到结果

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);

});

谢谢.

推荐答案

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

在这里,/ /g是一个正则表达式(正则表达式).标志g表示全局.这会导致所有匹配项被替换.

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

这篇关于如何替换字符串中的所有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆