Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我将值 abcdef 存储在 JavaScript 中的变量 x 中,并且我必须删除 abc。因此输出应该是def。如何解决这个问题?请不要通过要删除的字符数进行删除,因为我感兴趣的是删除特定的字符串,它可以是 3 或 4 个字符。所以,我需要一个更通用的方法来解决这个问题。
replace()可用于从字符串中替换(或删除)静态子字符串(或模式)。
replace()
以你的例子:
x = x.replace('abc', '');
console.log(x.substr(3));
或者
console.log(x.replace('abc', ''));