ECMAScript 6 中引入的 String.raw
原始字符串访问的实际用途是什么?
// String.raw(callSite, ...substitutions)
function quux (strings, ...values) {
strings[0] === "foo\n"
strings[1] === "bar"
strings.raw[0] === "foo\\n"
strings.raw[1] === "bar"
values[0] === 42
}
quux `foo\n${ 42 }bar`
String.raw `foo\n${ 42 }bar` === "foo\\n42bar"
我浏览了以下文档。
http://es6-features.org/#RawStringAccess
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw
http://www.2ality.com/2015/01/es6-strings.html
https://msdn.microsoft.com/en-us/library/dn889830(v=vs.94).aspx
The only the thing that I understand, is that it is used to get the raw string form of template strings and used for debugging the template string.
什么时候可以用在实时开发中?他们称其为标签函数。这是什么意思?
我缺少哪些具体用例?
最佳答案
我能想到的 String.raw
的最佳且几乎唯一的用例是,如果您尝试使用 Steven Levithan 的 XRegExp
library 之类的东西,接受带有明显反斜杠的文本。使用 String.raw 可以让您编写语义清晰的内容,而不必考虑加倍反斜杠,就像 JavaScript 本身的正则表达式文字一样。
例如,假设我正在网站上进行维护,并且发现了以下内容:
var isSingleUnicodeWord = /^\w+$/;
...旨在检查字符串是否仅包含“字母”。两个问题:A) 在人类语言领域,有数千个 \w
无法识别的“单词”字符,因为它的定义是以英语为中心的; B) 它包括 _
,许多人(包括 Unicode 联盟)认为它不是一个“字母”。
因此,如果我们在网站上使用 XRegExp
,因为我知道它支持 \pL
(\p
用于 Unicode 类别,并且L
代表“字母”),我可能会快速将其替换为:
var isSingleUnicodeWord = XRegExp("^\pL+$"); // WRONG
然后我想知道为什么它不起作用,捂脸,然后返回并转义那个反斜杠,因为它被字符串文字消耗了。
在这个简单的正则表达式中很容易,但在复杂的情况下,记住将所有这些反斜杠加倍是一种维护上的痛苦。 (只需询问尝试使用 Pattern
的 Java 程序员即可。)
输入String.raw
:
let isSingleUnicodeWord = XRegExp(String.raw`^\pL+$`);
示例:
let isSingleUnicodeWord = XRegExp(String.raw`^\pL+$`); // L: Letter
console.log(isSingleUnicodeWord.test("Русский")); // true
console.log(isSingleUnicodeWord.test("日本語")); // true
console.log(isSingleUnicodeWord.test("العربية")); // true
console.log(isSingleUnicodeWord.test("foo bar")); // false
<script src="https://cdnjs.cloudflare.com/ajax/libs/xregexp/3.1.1/xregexp-all.min.js"></script>
现在我只是放松一下并写下我的意思。我什至不必担心模板文字中使用的 ${...}
构造来进行替换,因为我想要应用量词 {...} 的可能性
到行尾断言 ($
) 的值...低。所以我可以愉快地使用替换,并且仍然不用担心反斜杠。可爱。
话虽如此,如果我经常这样做,我可能会想编写一个函数并使用标记模板而不是 String.raw
本身。但正确执行却出人意料地尴尬:
// My one-time tag function
function xrex(strings, ...values) {
let raw = strings.raw;
let max = Math.max(raw.length, values.length);
let result = "";
for (let i = 0; i < max; ++i) {
if (i < raw.length) {
result += raw[i];
}
if (i < values.length) {
result += values[i];
}
}
console.log("Creating with:", result);
return XRegExp(result);
}
// Using it, with a couple of substitutions to prove to myself they work
let category = "L"; // L: Letter
let maybeEol = "$";
let isSingleUnicodeWord = xrex`^\p${category}+${maybeEol}`;
console.log(isSingleUnicodeWord.test("Русский")); // true
console.log(isSingleUnicodeWord.test("日本語")); // true
console.log(isSingleUnicodeWord.test("العربية")); // true
console.log(isSingleUnicodeWord.test("foo bar")); // false
<script src="https://cdnjs.cloudflare.com/ajax/libs/xregexp/3.1.1/xregexp-all.min.js"></script>
如果您在很多地方使用它,也许这些麻烦是值得的,但对于一些快速的地方,String.raw
是更简单的选择。
关于javascript - ES6 Raw String Access 的实际用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34772762/