As shown in the image, when trying to create a regular expression in a lightning component controller, the save from the developer console fails because of an error parsing the data.
2 Answers
You'll want to log a bug with this; it appears that the quotes do need to be matched in Spring 17, and this behavior also does not appear in Winter 16. For now, you should be able to escape one quote to bring the balance back. Here's an example of a broken regex in Spring 17:
var r = /src="([^"]+)"/g;
Here's a way to fix it:
var r = /src="([^\"]+)"/g;
I did a simple test to make sure the results still worked:
var r = /src="([^\"]+)"/g;
var d = "<img src=\"www.google.com\" width=\"100\" height=\"100\" />";
console.log(r.exec(d));
Output:
[0]: src="www.google.com"
[1]: www.google.com
index: 5
Update
Gordon Oliver has this to say about the issue:
There are no known changes to the validation that would cause this, so we will open a bug and investigate to find the root cause.
-
1@DanielBallinger I was just about to notice that when you edited. Thanks for the edit, though, because I might have missed it for some time.– sfdcfox ♦Commented Jan 18, 2017 at 19:52
-
4I've raised this directly with the team that owns the Lightning compiler bits and asked them to comment. Commented Jan 18, 2017 at 21:47
-
1@DougChasman Thanks! This seems like it'll a pretty major bug if it gets to production.– sfdcfox ♦Commented Jan 18, 2017 at 21:54
-
2There are no known changes to the validation that would cause this, so we will open a bug and investigate to find the root cause. Commented Jan 19, 2017 at 20:22
-
1@DougChasman Thank you very much, as stated this is not a blocking issue since I can work around the regex issue, but I thought it important to raise the question! Commented Jan 19, 2017 at 20:37
This is actually a known bug, and it has a workaround, which is to use:
var r = new Regex("src=\"([^\"]+)\"/g");
instead of using the // syntax:
var r = /src="([^"]+)"/g;
I tried this on both Winter '16, and Spring '17, and both cases had the same exception for the expression syntax, and both were fixed using the new Regex syntax.
-
Ok thank you, I fixed it also by using
new Regexp()
Which seems to work or by changing the regex to a different format that does not break the salesforce validation. Commented Jan 19, 2017 at 21:01 -
@Gordon Oliver I have used the above said workaround but I am getting
Uncaught Action failed: c:Graph$controller$handleActive [Unable to evaluate code at: components/c/activityfeed.js]
error when loading the page.This is my regexvar token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g;
Made it like thisvar token = new Regex("d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|\"[^\"]*\"|'[^']*'/g");
Commented Oct 16, 2017 at 12:10