简体   繁体   English

如何在 javascript 字符串中拆分多个

[英]How to split multiple in javascript string

Hi I am new at learning JavaScript.嗨,我是学习 JavaScript 的新手。

Here is my string "Hello (mon) super" there is another string "Hello (wed) class"这是我的字符串“Hello (mon) super”还有另一个字符串“Hello (wed) class”

I wish to not have (mon) and (wed), and things beyond it.我希望没有(星期一)和(星期三),以及除此之外的东西。

I tried我试过了

let dummyString = 'Hello (wed) class';
dummyString = dummyString.split('(mon)')[0];
dummyString = dummyString.split('(wed)')[0];
console.log(dummyString);

this works perfect but when I use below code这很完美,但是当我使用下面的代码时

let dummyString = 'Hello (wed) class';
dummyString = dummyString.split('(mon),(wed)')[0];
console.log(dummyString);

it doesn't work它不起作用

A regex replacement is probably the most straightforward approach here:正则表达式替换可能是这里最直接的方法:

 var inputs = ["Hello (mon) super", "Hello (wed) class"]; inputs = inputs.map(x => x.replace(/\s*\(.*?\)\s*/, " ").trim()); console.log(inputs);

You can simply split with ( and ) with irrespective of the string it contains in between.您可以简单地用()拆分,而不管它包含的字符串是什么。

 let dummyString = 'Hello (wed) class'; console.log(dummyString.split(' (')[0], dummyString.split(') ')[1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM