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

Regex broken when using quotes and/or doublequotes #820

Open
cmcculloh-kr opened this issue Dec 14, 2020 · 0 comments
Open

Regex broken when using quotes and/or doublequotes #820

cmcculloh-kr opened this issue Dec 14, 2020 · 0 comments

Comments

@cmcculloh-kr
Copy link

Take the following valid JS:

const dom = require('sketch/dom');
const doc = dom.getSelectedDocument();
const targetName = "arbitraryName";
const newName = "arbitraryNewName";
const targetLayers = doc.getLayersNamed(targetName);
targetLayers.forEach(layer => layer.name = newName.replace(/['"]/, ''));

Sketch breaks when you try and run it (you get the following):

SyntaxError: Unexpected end of script
    at /Users/cmcculloh/Library/Application Support/com.bohemiancoding.sketch3/Plugins/Untitled.sketchplugin:7
Script executed in 5.573034s

So you have to double up the quotes, like this:

const dom = require('sketch/dom');
const doc = dom.getSelectedDocument();
const targetName = "arbitraryName";
const newName = "arbitraryNewName";
const targetLayers = doc.getLayersNamed(targetName);
targetLayers.forEach(layer => layer.name = newName.replace(/[''""]/, ''));

Note that this is just simplified example code that (yes I recognize) makes no sense (why would you want to run a replace on newName instead of just giving it the right name?), and is merely mean to illustrate the brokenness of Sketch's JS RexExp engine. My actual code is part of a plugin that, among other things, validates/fixes all layer names (removing spaces, em & en dashes, etc).

@cmcculloh-kr cmcculloh-kr changed the title Regex broken when using quotes and doublequotes Regex broken when using quotes and/or doublequotes Dec 14, 2020
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

2 participants