0

EDIT: 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+/);

//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', '');
//end loop(i)
//end hacks

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

Stupid...

Any help would be greatly appreciated, thanks!

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

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

PlexXoniC
  • 31
  • 5
  • 2
    Try `kwArr[i] = kwArr[i].trim().replace(/\.+/g, '');`. Or if you need triple dots, `/\.{3}/g` – Wiktor Stribiżew Apr 06 '18 at 13:45
  • @WiktorStribiżew ty but same results. – PlexXoniC Apr 06 '18 at 13:54
  • No way. Post the *actual* data in the question, not the images. – Wiktor Stribiżew Apr 06 '18 at 13:56
  • `kwArr[i] = kwArr[i].trim().replace(/\./g, '');` – Phius Apr 06 '18 at 13:57
  • @WiktorStribiżew This question hasn't already been answered, I've already tried: https://stackoverflow.com/questions/2390789/how-to-replace-all-dots-in-a-string-using-javascript – PlexXoniC Apr 06 '18 at 14:08
  • Huge log dumps are not encouraged in the question body. Post it on an external service and link to it. – Renato Gama Apr 06 '18 at 14:11
  • 1
    @RenatoGama fixed. – PlexXoniC Apr 06 '18 at 14:20
  • 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:00
  • If you tried that solution, you would see that `...` are removed. If there is an ellipsis, remove ellipses, `…` - then why are you even trying to remove a single ASCII dot? Study your string, try to solve the real problem, and only post a non-offtopic questions with [MCVE (minimal complete verifiable example)](http://stackoverflow.com/help/mcve). – Wiktor Stribiżew Apr 06 '18 at 20:06

0 Answers0