Word counting in HTML using regular expressions
P粉153503989
P粉153503989 2024-04-01 22:44:06
0
1
593

This is the same problem as this one. But since I'm not using javascript, "innerText" is not a solution for me and I want to know if the regex can combine /(<.*?>)/g and > /\S /g Get the actual word count without doing a bunch of string manipulation.

The language I'm using here is Dart, if a solution I haven't found already exists in it, that might also serve as an answer. Thanks!

Edit: Someone edited the tag? This question is not Dart specific but about regular expressions so I put them back as is.

Edit 2: The question is closed because it's not "focused" but I don't know how to make "if the regex can combine /(<.*?>)/g and /\S /g" More concentrated.

P粉153503989
P粉153503989

reply all(1)
P粉399090746

Assuming all text is contained within HTML elements, you can use (?<=>|\s)[^<\s>='"] ?(?=<|\s ).

Using strings <p>One</p><p>Two three, four. Five</p><p>Six</p> There are six games.

Notice:

  1. It uses backward groups, but not all browsers support this group.
  2. Punctuation marks at the end of words are grouped with them, such as "three", so keep this in mind if you plan on using actual words rather than just counting.
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template