Home > Back-end >  How to split multiple in javascript string
How to split multiple in javascript string

Time:02-04

Hi I am new at learning JavaScript.

Here is my string "Hello (mon) super" there is another string "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

CodePudding user response:

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);

CodePudding user response:

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])

  •  Tags:  
  • Related