1

When I use regexp in React-Native-Webview's injectedJavaScript prop, an error Unterminated regular expression literal error will be thrown and the regexp will not be honored.

How should I use regexp in injectedJavaScript? If not, is there a workaround I can use?

Minimal reproducible code is below:

// this won't work
<WebView
  source={{ uri: 'https://github.com/ashi009/node-fast-html-parser' }}
  injectedJavaScript={`const regE = /\n|\r/`}
/>

// this won't work as well
<WebView
  source={{ uri: 'https://github.com/ashi009/node-fast-html-parser' }}
  injectedJavaScript={`const regE = new RegExp(/\n|\r/);`}
/>

// this will work
<WebView
  source={{ uri: 'https://github.com/ashi009/node-fast-html-parser' }}
  injectedJavaScript={`const regE = /A*B*C*/`}
/>
  • OS: iOS
  • OS version: 13.3.1
  • react-native version: 0.61.2
  • react-native-webview version: 8.1.2

Check the bug screen below as well.

enter image description here

Full Error Message:

Error evaluating injectedJavaScript: This is possibly due to an unsupported return type. Try adding true to the end of your injectedJavaScript string. Error Domain=WKErrorDomain Code=4 "A JavaScript exception occurred" UserInfo={WKJavaScriptExceptionLineNumber=1, WKJavaScriptExceptionMessage=SyntaxError: Unterminated regular expression literal '/', WKJavaScriptExceptionColumnNumber=0, WKJavaScriptExceptionSourceURL=https://github.com/ashi009/node-fast-html-parser, NSLocalizedDescription=A JavaScript exception occurred}

6
  • It may be an escape character issue Commented Mar 29, 2020 at 5:38
  • could you elaborate? Commented Mar 29, 2020 at 5:45
  • Wild guess. Try a pattern without backslashes, see if it compiles. Can also put a ; at the end of the declaration Commented Mar 29, 2020 at 5:46
  • I could use a pattern without backslashes without error. Thanks! But I do need the patterns that have backslashes. What should I do then? Commented Mar 29, 2020 at 5:55
  • 1
    Sounds like it's being interpreted as an escape character or something in the webview, so a single backslash isn't enough in the pattern. Try double-escaping your backslashes, it may work: /\\n|\\r/; Commented Mar 29, 2020 at 5:57

1 Answer 1

4

You're not writing plain Javascript - you're writing Javascript inside the webview, through which your syntax has to be evaluated first. To use a literal backslash in injectedJavaScript (eg, for \r), put two backslashes instead (\\r):

injectedJavaScript={`const regE = new RegExp(/\\n|\\r/);`}

I think that

<WebView
  source={{ uri: 'https://github.com/ashi009/node-fast-html-parser' }}
  injectedJavaScript={`const regE = new RegExp(/\n|\r/);`}
/>

takes the \n in the string literal to be a newline in the code, and equivalent to:

<WebView
  source={{ uri: 'https://github.com/ashi009/node-fast-html-parser' }}
  injectedJavaScript={`const regE = new RegExp(/
|\r/);`}
/>

(and the same thing is done with the \r). But newlines aren't permitted in regular expression literals.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.