繁体   English   中英

删除单词和单词之间的空格以外的所有内容

[英]Remove everything except words and spaces between word

元素包含以下内容,其中所需的解决方案是清除“所有内容”,单词和空格之间的空格除外

 var textContent = "🇾🇹 +(269) Yukon Makukon" var cleanContent = textContent.replace(/[^A-Za-z]/g, ``) document.write(cleanContent); 

在您的正则表达式中添加空格字符,然后只需.trim()格式化即可。

 var textContent = "🇾🇹 +(269) Yukon Makukon" var cleanContent = textContent.replace(/[^A-Za-z ]/g, ``).trim(); document.write(cleanContent); 

let textContent = "🇾🇹 +(269) Yukon Makukon";
let result = textContent.replace(/[^a-zA-Z ]/g, "").trim();
console.log(result); // => Yukon Makukon

这将删除所有特殊字符以及所有多余的空格。

或者,您可以使用箭头函数并返回空字符串,或者如果只想忽略它们,则只需.replace(/[^A-Za-z ]/g, '')就可以了:

 var textContent = "🇾🇹 +(269) Yukon Makukon" // this way, you can replace them with any character if you wish. // you were ignoring space ---------------------V var cleanContent = textContent.replace(/[^A-Za-z ]/g, u=>''); document.write(cleanContent); 

 var cleanContent = textContent.replace(/[^a-zA-Z]/g, '').trim();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM