Quantcast
Channel: Active questions tagged react-native+ios - Stack Overflow
Viewing all articles
Browse latest Browse all 16750

How to use RegExp in React-Native Webview's injectedJavaScript prop?

$
0
0

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}


Viewing all articles
Browse latest Browse all 16750

Trending Articles