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.
我想删除字符串开头的“我会”。那些“我愿意”的词可能是大写/小写,所以我需要匹配不区分大小写。但我只想删除那些位于字符串开头的单词。
我尝试了 .replace(),但这并没有考虑到上述情况。
使用忽略大小写标志i:
i
var x = "I will write good code"; // the ^ at the start tells it to match from the beginning // the i flag tells it to ignore case x = x.replace(/^i\s+will\s+/i, ''); // Outputs: "write good code" regardless of the case of "i will"