如何使用js中的String.raw 函数

2025-10-22 02:25:27

1、语法

String.raw`templateStr`; 

String.raw(obj, ...substitutions);

2、参数

templateStr

必需。模板字符串。

obj

必需。一个使用对象文本表示法指定的格式正确的对象,例如 { raw: “value” }。

...substitutions

可选。一个数组(rest 参数),包含一个或多个替换值。

3、备注

String.raw 函数旨在与模板字符串一起使用。原始字符串将包含存在于字符串中的任何转义字符和反斜杠。

如果 obj 不是格式正确的对象,则会引发错误。

4、示例

function log(arg) {    

if(console && console.log) { 

        console.log(arg);   

 }

 }; 

 var name = "bob";

 log(`hello \t${name}`);

 log(String.raw`hello \t${name}`);

 // The following usage for String.raw is supported but 

// is not typical. 

log(String.raw({ raw: 'fred'}, 'F', 'R', 'E'));

 // Output:// hello   bob// hello \tbob// fFrReEd

5、要求

Microsoft Edge(Edge 浏览器)中受支持。应用商店应用(Windows 10 上的 Microsoft Edge)中也受支持。请参阅版本信息。

在以下文档模式中不受支持:Quirks、Internet Explorer 6 标准模式、Internet Explorer 7 标准模式、Internet Explorer 8 标准模式、Internet Explorer 9 标准模式、Internet Explorer 10 标准模式和 Internet Explorer 11 标准模式。在 Windows 8.1 中不受支持。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢