最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why does JavaScript's string.split() not work correctly in certain cases? - Stack Overflow

programmeradmin2浏览0评论

I need to split a string of text into its ponent words, so I'm using a Regex to split it on the empty spaces (in a Typescript file, btw).

splitIntoWords(text: string) : Array<string> {
    const separator = ' ';
    const words = text.split(new RegExp(separator, 'g'));
    return words;
}

This mostly works, but I've noticed that I regularly get words in the array that still contain spaces. If I copy the text into the Chrome console and split(' ') it I get the correct amount of words, but when I use the variable (even in the console) it invariably fails in some cases. I can't work out what the difference is. This is an example of my text:

"Le coronavirus en France : la décrue se poursuit en réanimation, la reprise économique au cœur des préoccupations. La mise en œuvre du plan de déconfinement élaboré par le gouvernement doit encore faire l’objet, jeudi, d’un « travail de concertation et d’adaptation aux réalités de terrain » avec les responsables et les élus locaux."

The regex never manages to split the substring "économique au" into two ponents, for instance. Does anyone know why this is happening?

I need to split a string of text into its ponent words, so I'm using a Regex to split it on the empty spaces (in a Typescript file, btw).

splitIntoWords(text: string) : Array<string> {
    const separator = ' ';
    const words = text.split(new RegExp(separator, 'g'));
    return words;
}

This mostly works, but I've noticed that I regularly get words in the array that still contain spaces. If I copy the text into the Chrome console and split(' ') it I get the correct amount of words, but when I use the variable (even in the console) it invariably fails in some cases. I can't work out what the difference is. This is an example of my text:

"Le coronavirus en France : la décrue se poursuit en réanimation, la reprise économique au cœur des préoccupations. La mise en œuvre du plan de déconfinement élaboré par le gouvernement doit encore faire l’objet, jeudi, d’un « travail de concertation et d’adaptation aux réalités de terrain » avec les responsables et les élus locaux."

The regex never manages to split the substring "économique au" into two ponents, for instance. Does anyone know why this is happening?

Share Improve this question edited Apr 29, 2020 at 23:05 Ralph Lavelle asked Apr 29, 2020 at 23:02 Ralph LavelleRalph Lavelle 5,7694 gold badges39 silver badges47 bronze badges 2
  • 2 have you try text.split(/\s/g) – MarkoCen Commented Apr 29, 2020 at 23:04
  • I cannot reproduce your problem. Your code works just fine for me. – Jörg W Mittag Commented Apr 30, 2020 at 7:03
Add a ment  | 

1 Answer 1

Reset to default 11

It sounds like the whitespace is occasionally not just a plain space. You can split on all whitespace by using \s for the separator instead, which will match any whitespace, including space characters and tab characters.

const text = "Le coronavirus en France : la décrue se poursuit en réanimation, la reprise économique au cœur des préoccupations. La mise en œuvre du plan de déconfinement élaboré par le gouvernement doit encore faire l’objet, jeudi, d’un « travail de concertation et d’adaptation aux réalités de terrain » avec les responsables et les élus locaux.";
const words = text.split(/\s/);
console.log(words);

Another option would be to use match instead of split, and match non-whitespace characters.

const text = "Le coronavirus en France : la décrue se poursuit en réanimation, la reprise économique au cœur des préoccupations. La mise en œuvre du plan de déconfinement élaboré par le gouvernement doit encore faire l’objet, jeudi, d’un « travail de concertation et d’adaptation aux réalités de terrain » avec les responsables et les élus locaux.";
const words = text.match(/\S+/g);
console.log(words);

发布评论

评论列表(0)

  1. 暂无评论