如何在JavaScript中将变量用作字符串和变量?

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

How to use a variable in Javascript as string and variable?

问题

以下是翻译好的代码部分:

var retrieveLocal = localStorage.getItem('transferData');
retrieveLocal = retrieveLocal.replaceAll(/\s+/g, '');
retrieveLocal = retrieveLocal.toLowerCase();
console.log(retrieveLocal);

请注意,我修复了代码中的一个错误,将 replaceAll 中的正则表达式改为 /\\s+/g 以匹配所有空格字符。

英文:

Is there a way to take a variable and use it as an input for the localStorage, then edit it to remove the spaces? What I have been trying to do is set a variable to the value of the local storage, then edit it. The problem is that Javascript won't allow me to use a string variable for this but I can't edit a normal variable. Here is my code:

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

<!-- language: lang-html -->

    var retrieveLocal = localStorage.getItem(&#39;transferData&#39;);
retrieveLocal = retrieveLocal.replaceAll(\s+ , &#39;&#39;);
  retrieveLocal = retrieveLocal.toLowerCase();
  console.log(retrieveLocal);

<!-- end snippet -->

The error I'm getting is SyntaxError: Unexpected string (at index.html:11:5)

答案1

得分: 1

<!-- 开始代码段: JavaScript 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->

localStorage.setItem('transferData', "This is the simple string");
var retrieveLocal = localStorage.getItem('transferData');
console.log('retrieveLocal', retrieveLocal);
const regex = / /g;
retrieveLocal = retrieveLocal.replaceAll(regex, '');
retrieveLocal = retrieveLocal.toLowerCase();
console.log(retrieveLocal);

<!-- 结束代码段 -->

<!-- 开始代码段: JavaScript 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->

localStorage.setItem('transferData', "This is the simple string");
var retrieveLocal = localStorage.getItem('transferData');
console.log('retrieveLocal', retrieveLocal);
const regex = / /g;
retrieveLocal = retrieveLocal.replaceAll(regex, '');
retrieveLocal = retrieveLocal.toLowerCase();
console.log(retrieveLocal);

<!-- 结束代码段 -->
英文:

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

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

localStorage.setItem(&#39;transferData&#39;, &quot;This is the simple string&quot;);
var retrieveLocal = localStorage.getItem(&#39;transferData&#39;);
console.log(&#39;retrieveLocal&#39;, retrieveLocal);
const regex = / /g;
retrieveLocal = retrieveLocal.replaceAll(regex, &#39;&#39;);
retrieveLocal = retrieveLocal.toLowerCase();
console.log(retrieveLocal);

<!-- end snippet -->

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

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

localStorage.setItem(&#39;transferData&#39;, &quot;This is the simple string&quot;);
var retrieveLocal = localStorage.getItem(&#39;transferData&#39;);
console.log(&#39;retrieveLocal&#39;, retrieveLocal);
const regex = / /g;
retrieveLocal = retrieveLocal.replaceAll(regex, &#39;&#39;);
retrieveLocal = retrieveLocal.toLowerCase();
console.log(retrieveLocal);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月22日 10:02:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528176.html
匿名

发表评论

匿名网友

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

确定