首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将文本拆分为javascript中的数组

将文本拆分为javascript中的数组
EN

Stack Overflow用户
提问于 2016-08-05 12:16:37
回答 6查看 82关注 0票数 1

当在javascript中获取文本中的,.时,我想要拆分。

我的短信是这样的:

猫爬上tree.In这句话,$U_SEL{}是个名词。

我要数组为:

代码语言:javascript
运行
复制
1.The cats climbed the tall tree.
2.In this sentence
3.$U_SEL{}
4.is a noun
EN

回答 6

Stack Overflow用户

发布于 2016-08-05 12:24:39

这个挑战的正则表达式是。

代码语言:javascript
运行
复制
var text = "The cats climbed the tall tree.In this sentence, $U_SEL{} is a noun."
var regex = /[.,]/;
text.split(regex);

有关regex的更多信息,请访问表达式

票数 1
EN

Stack Overflow用户

发布于 2016-08-05 12:24:55

这是regex。要在{}上拆分,首先将替换为{},{}.,然后尝试split。

代码语言:javascript
运行
复制
var str = "The cats climbed the tall tree.In this sentence, $U_SEL{} is a noun";
str = str.replace("{}", "{},");

//Array with splitted value
var result = str.split(/[,.]/g);

//Printing the result array
console.log(result);

票数 1
EN

Stack Overflow用户

发布于 2016-08-05 12:26:04

尝尝这个

代码语言:javascript
运行
复制
<script type="text/javascript">
    var text = "The cats climbed the tall tree.In this sentence, $U_SEL{}, is a noun";
    var spliteds = text.split(/[\.,]/);

    alert(spliteds[0]);
    alert(spliteds[1]);
    alert(spliteds[2]);
    alert(spliteds[3]);
</script>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38789041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档