如何使用javascript分割单词 [英] How to split words using javascript

查看:204
本文介绍了如何使用javascript分割单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个简单的问题,但是,我如何分割单词...例如

This might be a simple question but, how do i split words... for example

a = "even, test"

我使用 .split 来用空格分隔文本。

I have used .split to seperate the text with space.

所以结果就像

a = "even,"
b = "test"

但是,如何删除'逗号'在这里?

But, how do I remove the 'comma' here?

但在某些情况下,它可能会甚至测试,在某些情况下我可能会平衡,​​测试。一切都是动态的,所以我如何检查它们?

But in some conditions it might get "even test" and in some conditions i might get "even, test". All are dynamic, so how do i check it for both?

谢谢

推荐答案

首先, split()函数不是jQuery - 它是纯Javascript。

Firstly, the split() function is not jQuery - it is pure Javascript.

你有没有尝试用逗号和空格分割?在你的情况下,这样做会很好:

Did you try doing split with a comma and a space? That would have worked just fine in your case:

var result = input.split(', ');

对于更复杂的拆分,您可以使用正则表达式模式匹配来允许两者之间有多个逗号或空格字段:

For more complex splits, you can use regular expression pattern matching to allow multiple commas or spaces between the two fields:

var result = input.split(/[, ]+/);

但在你的情况下你可能不需要那么远。

but you probably don't need to go that far in your case.

这篇关于如何使用javascript分割单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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