-2

I am trying to split a string into an array of words that is present within []. Consider I have a string stating =>

const savedString = '[@hello hello] [@Bye bye], [@Friends forever] will miss you.'

Now I want to break the string into an array that will only contain.

const requiredArray = ['hello hello', 'Bye bye', 'Friends forever'];

I know about the split(), but only one delimiter can be passed. Need help.

https://www.w3schools.com/jsref/jsref_split.asp

3
  • Yet another example why w3schools is still a bad source. They mention that you can also use a regular expression but the examples don't show that... -> Have a look at String.prototype.split() on MDN Commented Feb 17, 2022 at 15:18
  • Please take a look at stackoverflow.com/questions/377961/… Commented Feb 17, 2022 at 15:19
  • A regular expression with .match() or .matchAll() would be another, and maybe easier, solution. Commented Feb 17, 2022 at 15:20

2 Answers 2

1

You can use .match() method with a regular expression with lookahead and lookbehind:

const savedString = '[@hello] [@Bye], [@Friends] will miss you.'

const n = savedString.match(/(?<=\[@)[^\]]*(?=\])/g);

console.log( n );

What of [@hello hell[@Bye bye] => Bye bye

If say for example the string is: '[@hello] [@Bye], [@Friends] will miss you [@hello hell[@Bye bye].'

One approach would be add to the above solution map() so each element is .split() at [@ and call pop() on the resulting array:

//starting with [ "hello", "Bye", "Friends", "hello hell[@Bye bye" ]
.map(word => 
    //split word into array
    word.split('[@')
    //[ ["hello"], ["Bye"], ["Friends"], ["hello hell", "Bye bye"] ]
    //Now take the last element
    .pop()
)
//Result: [ "hello", "Bye", "Friends", "Bye bye" ]

DEMO

const savedString = '[@hello] [@Bye], [@Friends] will miss you [@hello hell[@Bye bye].';

const n = savedString.match(/(?<=\[@)[^\]]*(?=\])/g).map(w => w.split('[@').pop());

console.log( n );

Note: Just so that you do not run into errors whenever there's no match, consider changing:

 savedString.match(/(?<=\[@)[^\]]*(?=\])/g)`

To:

(savedString.match(/(?<=\[@)[^\]]*(?=\])/g) || [])
Sign up to request clarification or add additional context in comments.

2 Comments

stackoverflow.com/users/3558931/peterkathanks for your response, any other regex if the string is [@hello hell[@Bye bye] but the string to be pushed to the array should be 'Bye bye'
I will give a solution that leaves the regex intact; usually you would consider all possible scenarios to help decide the best approach for a solution. Take a look at the second solution above.
0

You can achieve this with regex.

const savedString = '[@hello] [@Bye], [@Friends] will miss you.';
const regex = /\[@([a-zA-Z]+)\]/g;
const matches = savedString.match(regex);
const result = matches.map(match => match.replace(/\[@|\]/g, ''));
console.log(result);

Comments

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.