How to Split string In javascript Using regex

priyansh gupta

i am trying to do something like this

function findLongestWord(str) {
var wordContainer = str.split(/\b/) || 0;
document.write(wordContainer);
}

findLongestWord("The quick brown fox jumped over the lazy dog");

but this returns

The, ,quick, ,brown, ,fox, ,jumped, ,over, ,the, ,lazy, ,dog

however if i do something like this

function findLongestWord(str) {
var wordContainer = str.split(" ") || 0;
document.write(wordContainer);
}

findLongestWord("The quick brown fox jumped over the lazy dog");

it works as expected and returns

The,quick,brown,fox,jumped,over,the,lazy,dog

so why using /\b/ is different from using " " in split ?

adeneo

Because " " is a literal space, and \b is a word boundary.

Word boundaries occur before the first character in a string, if the first character is a word character, and then again after the last character in the string, if the last character is a word character, and also between two characters in the string, where one is a word character and the other is not a word character, meaning your string looks like this with the boundaries :

"The\b \bquick\b \bbrown\b \bfox\b \bjumped\b \bover\b \bthe\b \blazy\b \bdog"

In other words, you're matching the \b at the beginning of the word, and at the end of the word, and you're getting the spaces as well, as you're splitting, and end up with

["The"," ","quick"," ","brown"," ","fox"," ","jumped"," ","over"," ","the"," ","lazy"," ","dog"]

If you want to split words on boundaries, you have to add both of them, and anything in the middle, as in /\b.\b/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to split a string by length using split function and a RegEx in javascript?

From Dev

How to split string using regex in java

From Dev

How split a string using regex pattern

From Dev

How to split a string into 2 strings using RegEx?

From Dev

how to split a string using regex(.match())?

From Dev

How to split a string enclosed with "{}" and "[]" using regex expression

From Dev

How to split a string in c# using Regex?

From Dev

Javascript string split with regex

From Dev

javascript split() using regEx

From Dev

How to split string using regex to split between +,-,*,/ symbols?

From Dev

JavaScript Using Regex to split string by capital letters except when together

From Dev

How to split a huge string by date using JavaScript?

From Dev

String split via regex javascript

From Dev

regex split a html string in JavaScript?

From Dev

RegEx to split formatting string in JavaScript

From Dev

regex split string into groups in Javascript

From Dev

String split using RegEx in R

From Dev

Split string into sentences using regex

From Dev

Using String.split with a regex

From Dev

java - using regex to split string

From Dev

Python string split using regex

From Dev

java - using regex to split string

From Dev

PowerShell split string using RegEx

From Dev

Split and reformat a String using Regex

From Dev

String split with spaces using regex

From Dev

Split a string by spaces using regex

From Dev

Split string using regex python

From Dev

Javascript: using regex to split and store?

From Dev

How can I split string by same sequence of symbols using REGEX?