我有简单的字符串
@Autowiere
我想用 const string = 'or id 671674205=##=2012-02-03 18:35:34 SampleClass3 [DEBUG] detail for id 2146263624=##=2012-02-03 18:35:34 SampleClass1 [TRACE] verbose detail for id 619472373=##=2012-02-03 18:35:34 SampleClass5 [TRACE] verbose detail for id 1391429871=##=2012-02-03 18:35:34 SampleClass1 [DEBUG] detail for id 1522968690=##=2'
代替=##=
并尝试
<br/>
但是没有用...请帮助解决此问题
答案 0 :(得分:2)
因此,将修改后的string
分配为原始string
,您将必须将const
更改为let
或保存在其他variable
中。
let string = 'or id 671674205=##=2012-02-03 18:35:34 SampleClass3 [DEBUG] detail for id 2146263624=##=2012-02-03 18:35:34 SampleClass1 [TRACE] verbose detail for id 619472373=##=2012-02-03 18:35:34 SampleClass5 [TRACE] verbose detail for id 1391429871=##=2012-02-03 18:35:34 SampleClass1 [DEBUG] detail for id 1522968690=##=2';
string = string.replace(/=##=/g, 'aaa')
console.log(string)
答案 1 :(得分:1)
来自MDN文档(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace)
replace()
方法返回一个新字符串,其中包含部分或全部匹配项 用替换替换的模式。模式可以是字符串或 RegExp,并且替换可以是字符串或要调用的函数 每场比赛。
let string = 'or id 671674205=##=2012-02-03 18:35:34 SampleClass3 [DEBUG] detail for id 2146263624=##=2012-02-03 18:35:34 SampleClass1 [TRACE] verbose detail for id 619472373=##=2012-02-03 18:35:34 SampleClass5 [TRACE] verbose detail for id 1391429871=##=2012-02-03 18:35:34 SampleClass1 [DEBUG] detail for id 1522968690=##=2';
console.log("original string");
console.log(string)
string = string.replace(/=##=/g, 'aaa')
console.log("Updated string");
console.log(string)
答案 2 :(得分:1)
JavaScript字符串是不可变的。您不能更改字符串。字符串操作trim
slice
replace
返回新的字符串。你可以做到
const myString = string.replace(/=##=/g, '<br/>');
console.log(myString);
答案 3 :(得分:1)
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<exclusions>
<module name="com.google.guava"/>
</exclusions>
</deployment>
</jboss-deployment-structure>
方法返回与给定正则表达式匹配的替换字符串。在您的代码中,您缺少用替换后的值覆盖replace()
变量的值,因为您希望替换后的值位于string
变量中。因此,您需要
string