ES6 Raw String Access的实际用途是什么? [英] What are the actual uses of ES6 Raw String Access?

查看:384
本文介绍了ES6 Raw String Access的实际用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ECMAScript 6中引入了 String.raw Raw String Access的实际用途是什么?

  // String.raw(callSite,... substitution)

函数quux(strings,... values){
strings [0] === foo\\\

strings [1] ===bar
strings.raw [0] ===foo\\\\

strings.raw [1 ] ===bar
值[0] === 42
}

quux`foo \ $ {42} bar`

String.raw`foo\\\
$ {42} bar` ===foo\\\\
42bar



我浏览了以下文档。



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


我唯一了解的是,它用于获取原始字符串形式的模板字符串,并用于调试模板字符串


这可以用于实时开发吗?他们称这是一个标签功能。这是什么意思?



我缺少什么具体的用例?

解决方案

对于 String.raw ,最好的,非常接近的用例我可以想到,如果你想使用像Steven Levithan的 XRegExp ,它接受带有显着反斜杠的文本。使用 String.raw 可以让语义清楚地写出一些东西,而不必考虑将反斜杠加倍,就像您可以在JavaScript本身的正则表达式文字中一样。 p>

例如,假设我在网站上进行维护,我发现这一点:

  var isSingleUnicodeWord = / ^ \w + $ /; 

...这是为了检查一个字符串是否只包含字母。两个问题:A)在人类语言领域有数千个字字符, \w 不能识别,因为它的定义是以英语为中心的;和B)它包括 _ ,其中许多(包括Unicode联盟)认为不是一个信。



所以如果我们在网站上使用 XRegExp ,因为我知道它支持 \pL \ p 为Unicode类别,而 L 为letter),我可能会快速交换这个:

  var isSingleUnicodeWord = XRegExp(^ \pL + $); // WRONG 

然后我不知道为什么它不起作用,回避并逃避反斜杠,因为它被字符串字面量消耗。



在简单的正则表达式中容易,但在复杂的事情中,记住将所有这些反斜杠加倍是一个维护困难。 (只要求Java程序员尝试使用 Pattern 。)



输入 String.raw

  let isSingleUnicodeWord = XRegExp(String.raw` ^ \pL + $`); 

示例:



  let isSingleUnicodeWord = XRegExp(String.raw` ^ \pL + $`); // L:Letterconsole.log(isSingleUnicodeWord.test(Русский)); // trueconsole.log(isSingleUnicodeWord.test(日本语)); // trueconsole.log(isSingleUnicodeWord.test(العربية)); // trueconsole.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 本身。但是令人惊讶的是,要正确执行:



  //我的一次性标签function function xrex(strings,... values){let raw = strings.raw;令max = Math.max(raw.length,values.length); let result =; for(let i = 0; i  

 < script src =https: /cdnjs.cloudflare.com/ajax/libs/xregexp/3.1.1/xregexp-all.min.js\"></script>  



如果您在很多地方使用它,或许有一些快速的,那么麻烦是值得的, String.raw 是更简单的选项。


What are the actual uses of String.raw Raw String Access introduced in ECMAScript 6?

// 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"

I went through the below docs.

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.

When this can be used in real time development? They were calling this a tag function. What does that mean?

What concrete use cases am I missing?

解决方案

The best, and very nearly only, use case for String.raw I can think of is if you're trying to use something like Steven Levithan's XRegExp library that accepts text with significant backslashes. Using String.raw lets you write something semantically clear rather than having to think in terms of doubling your backslashes, just like you can in a regular expression literal in JavaScript itself.

For instance, suppose I'm doing maintenance on a site and I find this:

var isSingleUnicodeWord = /^\w+$/;

...which is meant to check if a string contains only "letters." Two problems: A) There are thousands of "word" characters across the realm of human language that \w doesn't recognize, because its definition is English-centric; and B) It includes _, which many (including the Unicode consortium) would argue is not a "letter."

So if we're using XRegExp on the site, since I know it supports \pL (\p for Unicode categories, and L for "letter"), I might quickly swap this in:

var isSingleUnicodeWord = XRegExp("^\pL+$"); // WRONG

Then I wonder why it didn't work, facepalm, and go back and escape that backslash, since it's being consumed by the string literal.

Easy enough in that simple regex, but in something complicated, remembering to double all those backslashes is a maintenance pain. (Just ask Java programmers trying to use Pattern.)

Enter String.raw:

let isSingleUnicodeWord = XRegExp(String.raw`^\pL+$`);

Example:

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>

Now I just kick back and write what I mean. I don't even really have to worry about ${...} constructs used in template literals to do substitution, because the odds of my wanting to apply a quantifier {...} to the end-of-line assertion ($) are...low. So I can happily use substitutions and still not worry about backslashes. Lovely.


Having said that, though, if I were doing it a lot, I'd probably want to write a function and use a tagged template instead of String.raw itself. But it's surprisingly awkward to do correctly:

// 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>

Maybe the hassle is worth it if you're using it in lots of places, but for a couple of quick ones, String.raw is the simpler option.

这篇关于ES6 Raw String Access的实际用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆