简体   繁体   English

Javascript - 从标题中删除特殊字符的正则表达式

[英]Javascript - Regex expression to remove special characters from title

I'm attempting to remove special characters from my title and convert it to a url-schema.我试图从我的标题中删除特殊字符并将其转换为 url-schema。 I'm able to accomplish this by using the .replace method such as: title.replace(/[^A-Za-z0-9\\-/s]/g, " ");我可以通过使用 .replace 方法来完成此操作,例如: title.replace(/[^A-Za-z0-9\\-/s]/g, " ");

I am running into problems when the title has parentheses in it.当标题中有括号时,我遇到了问题。 I am able to remove the parentheses but it then leaves an empty space at the end which then I am filling empty spaces with a - in order to create a URL schema, this is giving me some issues.我能够删除括号,但它会在最后留下一个空格,然后我用-填充空格以创建 URL 模式,这给我带来了一些问题。

How can I adjust my code below in order to remove the parentheses around (Cat and Dog) in order to not leave a space behind?如何调整下面的代码以删除(猫和狗)周围的括号,以免留下空格?

This is what's currently happening with my current code: "Pet Supplies Cat and Dog "这就是我当前代码的情况: "Pet Supplies Cat and Dog "

 let title = "Pet Supplies (Cat and Dog)" let cleanTitle = "" cleanTitle = title.replace(/[^A-Za-z0-9\\-/s]/g, " "); cleanTitle = cleanTitle.toLowerCase(); cleanTitle = cleanTitle.replace(new RegExp(" ", "g"), "-"); cleanTitle = cleanTitle.replace("-and", ""); cleanTitle = cleanTitle.replace(new RegExp("---", "g"), "--"); cleanTitle = cleanTitle.replace(new RegExp("--", "g"), "-"); console.log(cleanTitle)

My expected outcome is : pet-supplies-cat-dog我的预期结果是: pet-supplies-cat-dog

You can use您可以使用

 let title = "Pet Supplies (Cat and Dog)" title = title.toLowerCase() // Turn to lower .match(/[a-z0-9\\s-]+/g) // Extract all alnum + hyphen and whitespace chunks .map(x => x.trim().split(/\\s+/).join("-")) // Trim the items, split with whitespace and join with a hyphen .join("-") // Join the items with a hyphen .replace(/-and\\b/g, ''); // Remove whole word -and console.log(title);

There may be more elegant ways to do it, but you just want to remove special characters at the beginning and the end without adding a space character (or remove it after adding it).可能有更优雅的方法来做到这一点,但您只想删除开头和结尾的特殊字符而不添加空格字符(或添加后删除)。 That can be done with two additional replacements:这可以通过两个额外的替换来完成:

 let title = "Pet Supplies (Cat and Dog)" let cleanTitle = "" cleanTitle = title.replace(/[^A-Za-z0-9\\-/s]/g, " "); cleanTitle = cleanTitle.replace(/^ /g, ""); cleanTitle = cleanTitle.replace(/ $/g, ""); cleanTitle = cleanTitle.toLowerCase(); cleanTitle = cleanTitle.replace(new RegExp(" ", "g"), "-"); cleanTitle = cleanTitle.replace("-and", ""); cleanTitle = cleanTitle.replace(new RegExp("---", "g"), "--"); cleanTitle = cleanTitle.replace(new RegExp("--", "g"), "-"); console.log(cleanTitle)

You can achieve your output in this way:您可以通过以下方式实现输出:

 let title = "Pet Supplies (Cat and Dog)" let cleanTitle = "" cleanTitle = title.replace(/and/g,''); // removing all "and" cleanTitle = cleanTitle.replace(/\\s+/g, '-'); // replacing all spaces by "-" cleanTitle = cleanTitle.replace(/([()])/g, ''); // removing all "()" cleanTitle = cleanTitle.toLowerCase(); // converting to lowercases console.log(cleanTitle)

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

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