Search code examples
javascriptregexreplacesitefinity

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?


Solution

  • 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?