用jQuery中的新行替换结果集中的逗号 [英] Replacing commas in resultset with new line in jQuery

查看:123
本文介绍了用jQuery中的新行替换结果集中的逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去我不必做这样的事情,并且想知道是否确实有可能.我允许添加多个代码号,只要它们之间以逗号分隔即可.我要执行的操作是,用户单击确定"按钮,显示输入数字的a将使他们一个接一个地显示,并在其旁边显示删除"按钮.这部分很容易...难的部分是去除逗号,然后换行.

I have not had to do something like this in the past and am wondering if it is indeed possible. I am allowing multiple code numbers to be added in an so long as they are delimited by commas. What I am wanting to do is upon the user clicking on the "okay" button that a showing the numbers entered will show them one on top of each other with a "delete" button next to them. That part is easy...the hard part is getting the comma stripped out and the new line placed in its stead.

是否有任何人也可以指出我的例子或示例?

Are there any examples or samples that anyone can point me too?

推荐答案

您将使用正则表达式的"noreferrer> String#replace g标志(全局")作为搜索"部分,以及您选择的替换字符串(根据您的问题,我不确定您是否要<br>—例如HTML换行符—或\n实际上是换行符[但请记住,换行符被视为HTML中的空格]).例如:

You'd use String#replace with a regular expression using the g flag ("global") for the "search" part, and a replacement string of your choosing (from your question, I'm not sure whether you want <br> — e.g., an HTML line break — or \n which really is a newline [but remember newlines are treated like spaces in HTML]). E.g.:

var numbers = "1,2,3,4,5,6";
numbers = numbers.replace(/,/g, '<br>'); // Or \n, depending on your needs

或者,如果要允许空格,可以将可选空格放在正则表达式中逗号的两侧:

Or if you want to allow for spaces, you'd put optional spaces either side of the comma in the regex:

var numbers = "1,2,3,4,5,6";
numbers = numbers.replace(/ *, */g, '<br>'); // Or \n, depending on your needs

这篇关于用jQuery中的新行替换结果集中的逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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