Regex expression to replace special characters except first and last character found

huangapple go评论64阅读模式
英文:

Regex expression to replace special characters except first and last character found

问题

I'd like to remove every special character from a string identifier and replace them with hyphens so it can be URL friendly.

This is part of Sitefinity CMS URL configuration, meaning that every time I create an item, it gets the title of it and generates a URL slug based on the regex expression I provide.

So I can only use ONE regex expression, and ONE substitution text, since it is added in Sitefinity's CMS URL configuration fields.

I can't use code or use regex in multiple steps.

So, for example, if I have the following title string:
Infographic phishing's awareness and $prevention (updated)

I'd like it to transform to:
infographic-phishing-awareness-and-prevention-updated

In Settings / Advanced / System / Site URL Settings / URLRulesClient we have the default regex expression set:
[^\p{L}-!$()=@\d_'.]+|.+$

The problem is that when content is created, the URLs only replace spaces and not special characters, with hyphens.

Is there a way I can replace the last special characters at the end of the string with an empty space?

英文:

I'd like to remove every special character from a string identifier and replace them with hyphens so it can be URL friendly.

This is part of Sitefinity CMS URL configuration, meaning that every time I create an item, it gets the title of it and generates a URL slug based on the regex expression I provide.

So I can only use ONE regex expression, and ONE substitution text, since it is added in Sitefinity's CMS URL configuration fields.

I can't use code or use regex in multiple steps.

So, for example, if I have the following title string:
Infographic phishing's awareness and $prevention (updated)

I'd like it to transform to:
infographic-phishing-awareness-and-prevention-updated

In Settings / Advanced / System / Site URL Settings / URLRulesClient we have the default regex expression set:
[^\p{L}-!$()=@\d_'.]+|.+$

The problem is that when content is created, the URLs only replace spaces and not special characters, with hyphens.

Is there a way I can replace the last special characters at the end of the string with an empty space?

答案1

得分: 1

你可以尝试这个正则表达式 - 它匹配除了任何语言的字母、数字(0-9)、破折号、下划线之外的所有内容:

(?:'s)?[^\p{L}\-\d_]+|\.+$

如果你在Sitefinity中的标题是:
Infographic phishing's awareness and $prevention (updated).+!@=¨$'^^;,:

使用自定义正则表达式在Sitefinity中生成的URL将如下:
infographic-phishing-awareness-and-prevention-updated

测试正则表达式

在Sitefinity中的结果

如果你想在URL中保留点(.),你只需在方括号内添加它 - \.

(?:'s)?[^\p{L}\-\d_\.]+|\.+$

如果你想在URL中包含任何字符而不用破折号替换它们,只需在方括号之间添加它们 - 下面是一个示例,我包含了括号(我知道你想用破折号替换它们 - 只是作为参考的示例) - \(\)

(?:'s)?[^\p{L}\-\d_\(\)]+|\.+$

我在Sitefinity中测试了评论中的建议,但对我来说它们没有起作用。你在Sitefinity中测试过它们吗?

英文:

You can try this regex - it matches everything except any letters from any language, digits (0-9), dash, underscore:

(?:'s)?[^\p{L}\-\d_]+|\.+$

If your title in Sitefinity is:
Infographic phishing's awareness and $prevention (updated).+!@=¨$'^^;,:

The URL that will be generated by Sitefinity with the custom regex will be as below
infographic-phishing-awareness-and-prevention-updated

test the regex

result in Sitefinity

If you want to leave the dot (.) in the url you can just add it within the square brackets - \.

(?:'s)?[^\p{L}\-\d_\.]+|\.+$

If you want to include any characters in the url and not replace them with a dash just add them in between the square brackets - below is an example how I included the brackets (I know you want to replace them with dash - just as a sample for reference) - \(\)

(?:'s)?[^\p{L}\-\d_\(\)]+|\.+$

I tested in Sitefinity the suggestions from the comments but they didn't work for me. Did you test them in Sitefinity?

huangapple
  • 本文由 发表于 2023年5月11日 02:51:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定