[英]Object literal string escape math sign number
I have this string const myVar = '1-2'
I want to do spacing and make the variable to become '1 - 2'
.我有这个字符串
const myVar = '1-2'
我想做间距并使变量成为'1 - 2'
。
I tried this ${myVar.split("-")[0] - myVar.split("-")[1]}
, and the output I got is -1, because the - sign is being treated as math operator.我试过这个
${myVar.split("-")[0] - myVar.split("-")[1]}
,我得到的输出是 -1,因为 - 符号被视为数学运算符。 Any help?有什么帮助吗?
You could use .replace().您可以使用 .replace()。 Just assign this to a new variable, so
只需将其分配给一个新变量,因此
const myVar = '1-2';
let newVar = myVar.replace("-", " - ");
newVar will be the result of myVar with spacing on each side of the hyphen. newVar 将是 myVar 的结果,连字符的每一侧都有间距。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.