我刚开始学习JavaScript。我有一个段落,使用str.split('.')将其分割。此外,我需要在分割后的字符串中去掉引号。如何去掉它们?
我的妈妈站起来,从地上拿起一个盒子。“我们在美国,Rune。他们在这里讲英语。你一直在说英语,就像你一直在说挪威语一样。是时候用英语了。”
我希望结果如下:
我的妈妈站起来,从地上拿起一个盒子。我们在美国,Rune。他们在这里讲英语。你一直在说英语,就像你一直在说挪威语一样。是时候用英语了。
Your Answer
1 个回答
在拆分数组之前,去除所有引号会更容易。
const paragraph = `My mamma stood up and lifted a box off the ground. “We’re in America, Rune. They speak English here. You’ve been speaking English for as long as you’ve been speaking Norwegian. It’s time to use it.”`.replace(/“|”/g,''); console.log(paragraph); // "My mamma stood up and lifted a box off the ground. We’re in America, Rune. They speak English here. You’ve been speaking English for as long as you’ve been speaking Norwegian. It’s time to use it."
如果您坚持先拆分数组,那么您应该在.split之后循环/映射每个句子。
const sentences = `My mamma stood up and lifted a box off the ground. “We’re in America, Rune. They speak English here. You’ve been speaking English for as long as you’ve been speaking Norwegian. It’s time to use it.”`.split('.');
const result = result = sentences.map(sentence => sentence.replace(/“|”/g,''));
console.log(result);
/*
[
"My mamma stood up and lifted a box off the ground",
" We’re in America, Rune",
" They speak English here",
" You’ve been speaking English for as long as you’ve been speaking Norwegian",
" It’s time to use it",
""
];
*/
如您所见,最后一个项是空字符串。要去除它,您还可以使用.filter()。
result = sentences.map(sentence => sentence.replace(/“|”/g,'')).filter(sentence => sentence);
要去除空格,您还可以使用.trim()。
因此,将所有这些放在一起:
const sentences = `My mamma stood up and lifted a box off the ground. “We’re in America, Rune. They speak English here. You’ve been speaking English for as long as you’ve been speaking Norwegian. It’s time to use it.”`.split('.');
const result = sentences
.map(sentence => sentence.replace(/“|”/g, '').trim())
.filter(sentence => sentence);
console.log(result);
/*
[
"My mamma stood up and lifted a box off the ground",
"We’re in America, Rune",
"They speak English here",
"You’ve been speaking English for as long as you’ve been speaking Norwegian",
"It’s time to use it"
]
*/
Hot Questions
function_exists()无法判定自定义函数
2024-04-29 11:01:01
google 浏览器 手机版显示的怎么实现
2024-04-23 00:22:19
子窗口操作父窗口,输出没反应
2024-04-19 15:37:47
父窗口没有输出
2024-04-18 23:52:34
关于CSS思维导图的课件在哪?
2024-04-16 10:10:18
Hot Tools
vc9-vc14(32+64位)运行库合集(链接在下方)
phpStudy安装所需运行库集合下载
VC9 32位
VC9 32位 phpstudy集成安装环境运行库
php程序员工具箱完整版
程序员工具箱 v1.0 php集成环境
VC11 32位
VC11 32位 phpstudy集成安装环境运行库
SublimeText3汉化版
中文版,非常好用
热门话题
抖音等级价目表1-75
20337
7
20337
7
wifi显示无ip分配
13531
4
13531
4
虚拟手机号接收验证码
11851
4
11851
4
gmail邮箱登陆入口在哪里
8836
17
8836
17
windows安全中心怎么关闭
8420
7
8420
7
热门文章
2025年加密货币市场十大趋势预测:下一个风口在哪里?
2025-11-07
By DDD
币圈土狗项目如何识别?避免归零币的陷阱与风险预警
2025-11-07
By DDD
解决CSS @media 查询优先级与规则覆盖问题的教程
2025-11-07
By DDD
win10字体安装后在软件里找不到怎么办_win10字体安装与识别方法
2025-11-07
By DDD
铁路12306支付失败订单还在吗_铁路12306支付失败订单处理方法
2025-11-07
By DDD





