Home > Blockchain >  Convert a string into an array based on regex condition
Convert a string into an array based on regex condition

Time:11-28

Using JavaScript I'm creating a function that can take a string as a parameter and returns an array based on a regex condition. The string contains a pipe or | in certain places and I want to grab the string before the pipe gets iterated. To get a bit more clarity below is the visual representation

For example -

string: "Material: 100% Polyester | Three-button | Color: Gray"

resulted Array: ["Material: 100% Polyester", "Three-button", "Color Gray"]

Let me know if more information is needed

CodePudding user response:

use split

const list = "Material: 100% Polyester | Three-button | Color: Gray"

console.log(list.split(" | "))

  • Related