![]() |
JavaScript allows us to split the string into an array of words using string manipulation techniques, such as using regular expressions or the Table of Content Using the split() methodUsing s Example: The below code Splits a string into an array of words using the split() method.
Output [ 'GeeksforGeeks', 'is', 'a', 'leading', 'platform', 'that', '\nprovides', 'computer', 'science', 'resources' ] Using match() method with regular expressionThe regular expression can be used with the match() method to assert the word boundaries while ensuring that only the whole word is matched. This results in the array containing each word of the string as a separate element of the array. Example: The below code splits the string using the match() method along with the regular expression.
Output [ 'GeeksforGeeks', 'is', 'a', 'leading', 'platform', 'that', 'provides', 'computer', 'science', 'resources' ] Using spread operator with regex and match()Using Spread operator with regex can efficiently split a string into an array of words. This technique identifies word boundaries and spreads the matched substrings into an array of words. Example: To demonstrate splitting a sentence into an array of words using the spread operator
Output [ 'Geeks', 'for', 'Geeks' ] Using a For LoopIn this approach we iterate through each character using for loop and we check each character, if a space is encountered, it will be added to words array. If the character is not a space it will be added to the current word. Example: To demonstrate splitting a sentence into an array of words using For Loop.
Output [ 'Welcome', 'to', 'geeks', 'for', 'geeks' ] Using the reduce() MethodThe reduce() method can be used to split a string into an array of words by iterating through each character and building words based on spaces or other separators. This method provides a functional approach to string manipulation. Example: To demonstrate splitting a sentence into an array of words using the reduce() method
Output [ 'Hello', 'from' ] Using Array.from() MethodThe Array.from() method can be combined with a regular expression to convert a string into an array of words. This approach leverages the ability of Array.from() to create a new array from any iterable or array-like object. Example: The below code demonstrates how to split a string into an array of words using the Array.from() method with a regular expression.
Output [ 'GeeksforGeeks', 'is', 'a', 'leading', 'platform', 'that', 'provides', 'computer', 'science', 'resources' ] Using filter() Method with split()The filter() method can be used with the split() method to split a string into an array of words by filtering out any empty strings that may result from consecutive spaces or leading/trailing spaces. Example: To demonstrate splitting a sentence into an array of words using filter() method with split():
Output [ 'GeeksforGeeks', 'is', 'a', 'leading', 'platform', 'that', '\nprovides', 'computer', 'science', 'resources' ] Using Multiple DelimitersSometimes, splitting a string into words involves dealing with multiple types of delimiters such as spaces, commas, periods, etc. Using a regular expression within the split() method allows for handling multiple delimiters effectively. Example: Using Multiple Delimiters The following code demonstrates how to split a string into an array of words using split() with a regular expression that matches multiple delimiters.
Output [ 'Hello', 'world', 'How', 'are', 'you', 'today' ] Using the map() Method with trim()The map() method can be combined with the trim() method to split a string into an array of words and remove any leading or trailing whitespace from each word. This approach is useful when dealing with strings where words might be separated by spaces and have unnecessary whitespace. Example: To demonstrate splitting a sentence into an array of words using map() method with trim():
Output [ 'GeeksforGeeks', 'is', 'a', 'leading', 'platform', 'that', 'provides', 'computer', 'science', 'resources' ] Using flatMap() MethodThe flatMap() method first maps each element using a mapping function, then flattens the result into a new array. This approach can be leveraged to split a string into an array of words and filter out any empty strings in one step. Example: The below code demonstrates splitting a sentence into an array of words using the flatMap() method.
Output [ 'GeeksforGeeks', 'is', 'a', 'leading', 'platform', 'that', '\nprovides', 'computer', 'science', 'resources' ] |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |