要求对fixedEncodeURIComponent进行解释 [英] Explanation requested for fixedEncodeURIComponent

查看:178
本文介绍了要求对fixedEncodeURIComponent进行解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以向我解释此功能?我已经对其进行了测试,它的工作就像一个梦,但我不知道该怎么做!

I'm wondering if anyone can explain this function to me? I've tested it and it works like a dream but I don't understand how!

它来自此处的MDN参考: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

It's from the MDN reference here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

function fixedEncodeURIComponent (str) {
  return encodeURIComponent(str).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
}

我将替换理解为替换之后的匹配,我苦苦挣扎的是转义引用和辅助替换,这导致实际编码值替换了字符,例如(=%28 and)=%29.

I understand replace as the match followed by the replacement, what I am struggling with is the escape reference and the secondary replacement which results in actual encode values replacing characters e.g. ( = %28 and )= %29.

推荐答案

对转义"的引用只是对该名称的全局函数的引用.如果.replace()的第二个参数是一个函数,则JavaScript将匹配的字符串传递给该函数,并将其替换为该函数返回的任何内容.

The reference to "escape" is just a reference to the global function of that name. If the second argument to .replace() is a function, then JavaScript passes the matched string to the function and replaces it with whatever the function returns.

尝试输入

escape("!")

在浏览器的控制台中.

这篇关于要求对fixedEncodeURIComponent进行解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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