Method 1: Utilize the split function
This method breaks a given string around regular expression matches. This method returns a string array after splitting against the given regular expression.
We will have the syntax as below:
Separator: Unrequired field. This gives instructions on which character or regular expression should be used to split the string. The whole string will be given back.
Limit: Unrequired field. The split limit is indicated by an integer, and items that exceed it will not be included in the array list.
When a substring is discovered, a separator is removed, and the substrings are returned in an array.
let string = "welcome awesome, Message!" string.split(/[\s,]+/) // welcome,awesome,Message!
Method 2: Utilize Array join() function
The array’s items can be combined into a string using the Array join() function. This method creates a string by adding the components of an array, and then it returns the result. A unique separator is used to divide the elements.
This one has the syntax as follows:
As previously noted, this function accepts two parameters.
Array: The array that will be joined
Separator: The separator to be utilized is specified. If not, the comma-separator by default is used.
Apply that method to the script below:
As you can see, the string has now been split with several separators.