Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buf if word start with "+" char #16

Open
alainib opened this issue Jul 27, 2018 · 6 comments
Open

buf if word start with "+" char #16

alainib opened this issue Jul 27, 2018 · 6 comments

Comments

@alainib
Copy link

alainib commented Jul 27, 2018

hello.

There is a bug if the text to higliht start with a "+"
for example

const display = "+ Laburnocytisus adami" ;

<Highlighter
    highlightStyle={thisstyles.highlighter}
    searchWords={[this.state.valueForModal]}
    textToHighlight={display}
    style={thisstyles.fontSize15}
 />

return this error :

   SyntaxError: SyntaxError: Invalid regular expression: /+/: Nothing to repeat

any idea to fix it ? thx

@fungilation
Copy link

Tested "+ Laburnocytisus adami" as textToHighlight prop, cannot reproduce.

@alainib
Copy link
Author

alainib commented Aug 3, 2018

hello, the problem is when searchWords contain "+"
this is how i get the error :

  this.state.valueForModal = "+ Lab"; // the content is filled from user input searching for string starting with '+ lab'
  const display = "+ Laburnocytisus adami" ;
 
 <Highlighter
                  highlightStyle={thisstyles.highlighter}
                  searchWords={[this.state.valueForModal]}
                  textToHighlight={display}
                  style={thisstyles.fontSize15}
                />

@lytieunuong1
Copy link

lytieunuong1 commented Oct 27, 2018

I got that bug when I type "+" or "*" into the search words.
I think you should catch some special characters in Regular expression
For hotfix we can replace the search key before assign to the searchWords like the code bellow:

let searchKey = props.searchKey ? props.searchKey : ""
  const specialKey=["[", "?", "+", "*"]`
  for(i = 0 ; i < specialKey.length; i++ ) {
    searchKey = searchKey.replace(specialKey[i],'\\'+specialKey[i])
  }

@fungilation
Copy link

I tested with this:

<Highlighter
  style={{ color: '#888', fontSize: 13, fontFamily: 'System', padding: 10 }}
  highlightStyle={{ backgroundColor: '#FEF3A2' }}
  numberOfLines={3}
  ellipsizeMode="middle"
  searchWords={["+ Lab", "* Lab", "? Lab", "[ Lab"]}
  textToHighlight={"+ Laburnocytisus adami"}
  autoEscape={true}
/>

I get:

image

No error, so I still can't reproduce. My env:

React Native 0.57.0

> react-native info

  React Native Environment Info:
    System:
      OS: Windows 10
      CPU: x64 Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
      Memory: 4.65 GB / 15.95 GB
    Binaries:
      Yarn: 1.12.1 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
      npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD

@CyxouD
Copy link

CyxouD commented Feb 3, 2020

autoEscape={true} helps to avoid these types of errors if you don't need to process regular expressions, maybe make it true by default?

@xCTAPx
Copy link

xCTAPx commented Jun 24, 2021

Yes, it would be nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants