0

I'm using a webcrawler i made to fetch some data, and sometimes i need to check if a string contains another. The current method i'm using for it is 'includes' like this:

if(biggerString.includes(string){
   //do my thing
}

But sometimes it doesn't work. Here's an example:

Bigger string: 'iphone 6 apple com 16gb tela 47” ios 8 touch id câmera isight 8mp wi-fi 3g4g gps mp3 bluetooth e nfc - cinza espacial'
Substring: 'iphone 6'

I tried to remove any special characters that could interfere like this:

let biggerString2 = biggerString.replace(/[&\/\\#,()$~%.'":*?<>{}]/g, '').trim().toLowerCase();
let substring2 = substring.replace(/[&\/\\#,()$~%.'":*?<>{}]/g, '').trim().toLowerCase();

But it still doesn't work. What am i missing here?

Joao Victor
  • 1,111
  • 4
  • 19
  • 39
  • 3
    Your `iphone 6` example does in fact work. – Pointy Jan 21 '19 at 14:18
  • Possibly the white space in "iphone 6" in one of the strings is a different space, like a non-breaking space. You could try `if (/iphone\s6/.test(biggerString))`. – trincot Jan 21 '19 at 14:19
  • I've tested it in another script and it does work. But when i try it in my webcrawler, it doesn't. – Joao Victor Jan 21 '19 at 14:19
  • All strings contain other strings, what do you mean? – Liam Jan 21 '19 at 14:20
  • Probably there is some kind of special char, I think it would be nice to do something like https://stackoverflow.com/a/23994336/4229159 basically escaping anything that could make you have issues with the output. Also, it could be that the page doesn't really render all of the content depending on the user agent or similar, and it just prints a part of the output – Alejandro Vales Jan 21 '19 at 14:21
  • might not be the issue but i think you're missing an `)` in `if(biggerString.includes(string){ //...` (should be `if(biggerString.includes(string)){ //...`) – Ahmed Bajra Jan 21 '19 at 15:42
  • It was just a typo, @AhmedBajra – Joao Victor Jan 21 '19 at 15:43

0 Answers0