-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

  • 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()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) on MDN – Andreas Feb 17 '22 at 15:18
  • Please take a look at https://stackoverflow.com/questions/377961/efficient-javascript-string-replacement/378000#378000 – James Feb 17 '22 at 15:19
  • A regular expression with [`.match()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@match) or [`.matchAll()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@matchAll) would be another, and maybe easier, solution. – Andreas Feb 17 '22 at 15:20

2 Answers2

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) || [])
PeterKA
  • 24,158
  • 5
  • 26
  • 48
  • https://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' – Parul Ranjan Feb 18 '22 at 09:43
  • 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. – PeterKA Feb 19 '22 at 23:07
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);
Parvesh Kumar
  • 1,104
  • 7
  • 16