[英]Javascript regular expression: Match all alphanumeric characters, allow dashes spaces and underscores except all whitespace
[英]Remove all characters except alphanumeric and spaces with javascript
input.replace(/[^\w\s]/gi, '')
無恥地從另一個答案中竊取。 ^
在字符類中的意思是“不是”。 所以這是“不是” \\w
(相當於\\W
)而不是\\s
,它是空格字符(空格、制表符等)你可以只使用文字如果你需要。
我知道這是一個舊線程,但非常受歡迎,出現在 Google 搜索的頂部。 因此,作為替代方案,3limin4t0r 接受的答案和評論啟發了我:
.replace(/\W+/g, " ")
恕我直言
const input = document.querySelector("input"); const button = document.querySelector("button"); const output = document.querySelector("output"); button.addEventListener("click", () => { output.textContent = input.value.replace(/\\W+/g, " "); })
<input> <button>Replace</button> <p> <output></output> </p>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.