0

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

1 Answer 1

4

use split

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you it worked. There were some white space and new line were added to the array elements after applying split method but the .trim() function able to remove all of it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.