1

Not a duplicate, I've already tried the possible solutions at:

How to replace all dots in a string using JavaScript

I've having a JS issue with string.replace not replacing periods/ellipsis, maybe I'm just really tired but it's annoying the **** out of me. I'd appreciate any suggestions. This is in a chrome extension content script if that has any relevance.

I've tried variations of the below code and .split, other regex, etc. with no luck. All content of document.body.innerText.toLowerCase().trim().split(/\s+/); is from the Google homepage after searching for the keyword "google".

Relevant code:

var kwArr = document.body.innerText.toLowerCase().trim().split(/\s+/);

//remove empties, links, etc here...

//stupid period/ellipsis...
//begin loop(i)
kwArr[i] = $.trim(kwArr[i].replace(/\.+$/g, ''));
kwArr[i] = kwArr[i].replace('.', '');
kwArr[i] = kwArr[i].replace("\./g", "");
kwArr[i] = kwArr[i].replace('/\./g', '');
kwArr[i] = kwArr[i].replace('/\u2E7F\/g', '');
kwArr[i] = kwArr[i].replace('/\u2E7F\/g', '');
kwArr[i] = kwArr[i].trim().replace(/\.+/g, '');
kwArr[i] = kwArr[i].trim().replace(/\.{3}/g, '');
//end loop(i)
//end hacks

Array dump with frequency after running kwArr through replace, etc.:

Stupid...

Data from console.log(JSON.stringify(kwArr)):

https://gist.github.com/PlexXoniC/5084ff5fb69785f60bd043a205923601

Any help would be greatly appreciated, thanks!

PlexXoniC
  • 31
  • 5
  • 1
    Some of the strings contain unicode ellipsis `\u2026`, there's even a middledot `\u00B7`. Also, you can split by `/[.\u2026\u00b7]*\s+/` to skip the loop (the last word will still need a replace). – wOxxOm Apr 06 '18 at 15:49
  • @wOxxOm - perfect, thank you! Split worked perfectly for combining everything into one. `/[.\u2026\u00b7]*\s+/` – PlexXoniC Apr 06 '18 at 16:20

0 Answers0