简体   繁体   中英

What does this syntax mean JAVASCRIPT

I was going through some open source codes when I stumbled upon this library : he .

In the source code written in javascript it has statements like this:

var regexInvalidRawCodePoint = /<%= regexInvalidRawCodePoints %>/;

I can't figure out what it means. Tried googling it too.

Btw it's not standard regex. The script is not trying to match regexInvalidRawCodePoint .

  • regexInvalidRawCodePoint is a variable
  • /<%= regexInvalidRawCodePoints %>/ is a regular expression (with no options)

<%= and %> are not Javascript. They are tags that replace regexInvalidRawCodePoints with the value of regexInvalidRawCodePoints when pre-processing, before the javascript is evaluated.

So the value of regexInvalidRawCodePoint will be used as the REGEX after it is preprocessed, and that regex will be assigned to the variable regexInvalidRawCodePoint

In the package.json you have a build script:

"build": "grunt build"

This build script will create the he.js file out of the src/he.js file.

In the build process the line:

var regexInvalidRawCodePoint = /<%= regexInvalidRawCodePoints %>/;

will become

var regexInvalidRawCodePoint = /[\0-\x08\x0B\x0E-\x1F\x7F-\x9F\uFDD0-\uFDEF\uFFFE\uFFFF]|[\uD83F\uD87F\uD8BF\uD8FF\uD93F\uD97F\uD9BF\uD9FF\uDA3F\uDA7F\uDABF\uDAFF\uDB3F\uDB7F\uDBBF\uDBFF][\uDFFE\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/;

The responsible grunt build task is the template which uses the script ./scripts/export-data.js . That script contains:

'regexInvalidRawCodePoints': require('./invalid-code-points-regex.js'),

The <%= regexInvalidRawCodePoints %> is a placeholder that will be replace with the content of ./invalid-code-points-regex.js

This is nothing JavaScript specific, but a placeholder syntax defined by eg a template language.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM