简体   繁体   English

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

[英]Remove everything except words and spaces between word

The element contains the following content, where the desired solution is to clean "everything" except the words and space between 元素包含以下内容,其中所需的解决方案是清除“所有内容”,单词和空格之间的空格除外

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

Add space character in your regex and then just .trim() it to format it nicely. 在您的正则表达式中添加空格字符,然后只需.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

This will remove all special characters along with any extra white space. 这将删除所有特殊字符以及所有多余的空格。

Or, you may use an arrow function and return empty string or just .replace(/[^A-Za-z ]/g, '') will be fine if you just want to ignore them: 或者,您可以使用箭头函数并返回空字符串,或者如果只想忽略它们,则只需.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