如何用数组对象替换我的字符串中的子字符串?

huangapple go评论101阅读模式
英文:

How do I replace substrings in my string with array objects?

问题

我正在尝试替换字符串中的特殊字符,字符串是文件名,因为特殊字符无法存储在Windows文件系统上。

我尝试过的方法只是简单地使用 replace() 函数重复地对我的变量进行替换,直到覆盖我想要替换的所有特殊字符。示例代码:

// 格式化文件名以保存到文件系统
function formatFileName(postTitle, postUrl, nsfw) {
    return new Promise(resolve => {
        // 过滤掉不良字符
        postTitle = postTitle.replace(/\?/g, "[q]");
        postTitle = postTitle.replace(/\//g, "
展开收缩
"
);
postTitle = postTitle.replace(/\</g, "[l]"); postTitle = postTitle.replace(/\>/g, "[m]"); postTitle = postTitle.replace(/\"/g, "[quo]"); postTitle = postTitle.replace(/\*/g, "[st]"); // 继续替换其他特殊字符... resolve(postTitle); }); }

尽管这样可以工作,但我知道肯定有更好的方法可以做到这一点,因此我想找出如何以更干净的方式完成。

我尝试通过以下方式来改进代码:

let specialCharacters = ["?", "/", "<", ">", "\"", "*", "\\"]; 
let replacement = ["[q]", "
展开收缩
"
, "[l]", "[m]", "[quo]", "[st]", "[bs]"];
// 将特殊字符替换为文件系统兼容的字符 for (var i = 0; i < specialCharacters.length; i++) { postTitle = postTitle.split(specialCharacters[i]).join(replacement[i]); }

然而,使用新修改的代码,它不会替换字符串中的任何特殊字符。我还尝试了以下方式:

let specialCharacters = [/\?/g, /\//g, /\<\//g, /\>\//g, /\"\//g, /\*\//g, /\\\//g]; 
let replacement = ["[q]", "
展开收缩
"
, "[l]", "[m]", "[quo]", "[st]", "[bs]"];
// 将特殊字符替换为文件系统兼容的字符 for (var i = 0; i < specialCharacters.length; i++) { postTitle = postTitle.replace(specialCharacters[i], replacement[i]); } console.log(postTitle);

但结果没有改变。我还尝试将其包装在一个异步/等待函数中,因为我认为可能是因为NodeJS没有等待循环完成,但这也没有改变任何事情。

请问有人能告诉我我做错了什么吗?

英文:

I am trying to replace special characters in a string, with the string being file names as special characters cannot be stored on the Windows filesystem.

What I have tried is simply using replace() on my variable repeatedly until it covers all of the special characters I want to replace. Example code:

//Formats file name to save to Filesystem
function formatFileName(postTitle, postUrl, nsfw) {
return new Promise(resolve =&gt; {
	//Filter out bad characters
	postTitle = postTitle.replace(/\?/g, &quot;[q]&quot;);
	postTitle = postTitle.replace(/\//g, &quot;
展开收缩
&quot;); postTitle = postTitle.replace(/\&lt;/g, &quot;[l]&quot;); postTitle = postTitle.replace(/\&gt;/g, &quot;[m]&quot;); postTitle = postTitle.replace(/\&quot;/g, &quot;[quo]&quot;); postTitle = postTitle.replace(/\*/g, &quot;[st]&quot;);

While this works, I know that there is definitely a better way out there to do this, and as such, I'd like to find out how I could do so in a cleaner manner.

I've tried refining the code by doing this instead:

    let specialCharacters = [&quot;?&quot;, &quot;/&quot;, &quot;&lt;&quot;, &quot;&gt;&quot;, &quot;\&quot;&quot;, &quot;*&quot;, &quot;\\&quot;];
	let replacement = [&quot;[q]&quot;, &quot;
展开收缩
&quot;, &quot;[l]&quot;, &quot;[m]&quot;, &quot;[quo]&quot;, &quot;[st]&quot;, &quot;[bs]&quot;]; //Replace special characters into filesystem-compatible ones for (var i = 0; i &lt; specialCharacters.length; i++) { postTitle.replace(specialCharacters[i], replacement[i]); }

However, with the newly modified code, it does not replace any of the special characters in the string. I've also tried doing

    let specialCharacters = [/\?/g, /\//g, /\&lt;/g, /\&gt;/g, /\&quot;/g, /\*/g, /\\/g];
	let replacement = [&quot;[q]&quot;, &quot;
展开收缩
&quot;, &quot;[l]&quot;, &quot;[m]&quot;, &quot;[quo]&quot;, &quot;[st]&quot;, &quot;[bs]&quot;]; //Replace special characters into filesystem-compatible ones for (var i = 0; i &lt; specialCharacters.length; i++) { postTitle.replace(specialCharacters[i], replacement[i]); } console.log(postTitle); resolve(postTitle);

but the outcome did not change. Another thing I tried was to wrap it in an async/await function as I thought that it may be because NodeJS wasn't waiting for the loop to finish, but that did not change anything either.

Could someone please tell me what I'm doing wrong?

答案1

得分: 2

JavaScript中的字符串是不可变的。您没有重新分配replace返回的值。

let specialCharacters = [/\?/g, /\//g, /\&lt;/g, /\&gt;/g, /\&quot;/g, /\*/g, /\\/g];
let replacement = ["[q]", "
展开收缩
"
, "[l]", "[m]", "[quo]", "[st]", "[bs]"];
//将特殊字符替换为与文件系统兼容的字符 for (var i = 0; i < specialCharacters.length; i++) { // 将返回值赋给postTitle postTitle = postTitle.replace(specialCharacters[i], replacement[i]); } console.log(postTitle);
英文:

Strings in JavaScript are immutable. You are not reassigning the returned value from replace.

let specialCharacters = [/\?/g, /\//g, /\&lt;/g, /\&gt;/g, /\&quot;/g, /\*/g, /\\/g];
let replacement = [&quot;[q]&quot;, &quot;
展开收缩
&quot;, &quot;[l]&quot;, &quot;[m]&quot;, &quot;[quo]&quot;, &quot;[st]&quot;, &quot;[bs]&quot;]; //Replace special characters into filesystem-compatible ones for (var i = 0; i &lt; specialCharacters.length; i++) { // Assign return value to postTitle postTitle = postTitle.replace(specialCharacters[i], replacement[i]); } console.log(postTitle);

答案2

得分: 0

以下是您要翻译的内容:

var specialCharacters = ['?', '/', '<', '>', '"', '*', '\\'];
var replacement = ["[q]", "
展开收缩
"
, "[l]", "[m]", "[quo]", "[st]", "[bs]"];
var pattern = new RegExp(specialCharacters.map(x => '(' + x + ')').join('|'), 'g'); var str = "<span>What is your question?</span>"; console.log(str.replace(pattern, x => replacement[specialCharacters.indexOf(x)]));

请注意,我只提供代码的翻译部分,不包括其他内容。

英文:

You can try this way:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var specialCharacters = [&#39;?&#39;, &#39;/&#39;, &#39;&lt;&#39;, &#39;&gt;&#39;, &#39;&quot;&#39;, &#39;*&#39;, &#39;\\&#39;];
var replacement = [&quot;[q]&quot;, &quot;
展开收缩
&quot;, &quot;[l]&quot;, &quot;[m]&quot;, &quot;[quo]&quot;, &quot;[st]&quot;, &quot;[bs]&quot;]; var pattern = new RegExp(specialCharacters.map(x =&gt; &#39;(\\&#39; + x + &#39;)&#39;).join(&#39;|&#39;), &#39;g&#39;); var str = &quot;&lt;span&gt;What is your question?&lt;/span&gt;&quot;; console.log(str.replace(pattern, x =&gt; replacement[specialCharacters.indexOf(x)]));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 23:56:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定