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.
如何替换所有非空格字符 (\s) 的非单词字符 (\W)?
这是所需的功能:
"the (quick)! brown \n fox".gsub(regex, "#")
=>
"the #quick## brown \n fox"
"the (quick)! brown \n fox".gsub(/[^\w\s]/, "#")
通过使正则表达式替换不是单词字符或空格字符的任何内容。
我认为你需要一个像这样的正则表达式:
/[^\w\s]/
当您将抑扬符添加^到字符集的开头时,它会否定表达式,以便匹配集中字符以外的任何内容。
^