'How to split multiple in javascript string
Hi I am new at learning JavaScript.
Here is my string "Hello (mon) super (Henry)" there is another string "Hello (wed) class (Mario)"
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
Solution 1:[1]
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);
Solution 2:[2]
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])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tim Biegeleisen |
| Solution 2 | Rohìt JÃndal |
