4

I have a validation function in lightning component that works on regex. I have a regular expression to check existance of special symbols but I am not able to save my js controller with that regular expression.

Field Integrity Exception: org.auraframework.util.json.JsonStreamReader$JsonStreamParseException: Unterminated string [663, 65]: ';'<>?,./]/;': Source

var regularExpression; 
regularExpression = /[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]/; //This line is giving error when trying to save 
if(!regularExpression.test(value)) {
     //Action to be taken
}

The code works fine with some other regex's

1
  • Looks like the compiler doesn't like this. Did you try to use the RegExp constructor? Commented Sep 19, 2017 at 7:39

2 Answers 2

5

Try using the constructor syntax.

I've verified that this saves in lightning:

var regularExpression; 
var re = new RegExp('[-!$%^&*()_+|~=`{}\[\]:";\'<>?,.\/]');

Note the escape of the ' char within the expression.

2
  • Hi Caspar Harmer, thanks for the reply. But the same expression isn't working if passed to RegExp constructor in single quotes.
    – Shri
    Commented Sep 19, 2017 at 10:01
  • FYI for others who come here, this has a Salesforce Known Issue that's still "In Review", if you have a minute to go indicate that this affects you, it will help Salesforce see the impact that this has on the broader developer community! success.salesforce.com/issues_view?id=a1p3A0000003Ue8QAE
    – zachelrath
    Commented Jul 18, 2019 at 16:25
0

You need to double the backslash in your expression to make it work.

var regularExpression;

var re = new RegExp('[-!$%^&*()_+|~=`{}\\[\\]:";\\'<>?,.\\/]');

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .