Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Murray Roke 502 posts 965 karma points c-trib
    May 07, 2020 @ 06:15
    Murray Roke
    0

    UrlReplacing in javascript

    Hi,
    We have developed a preview of what a google search result would look like that updates as the user types (titles, descriptions, URL) but I'm having trouble with the urlReplacing in v8

    Is there a reasonable regex or similar I could use to predict what the friendly URL will be after it's published?

    It doesn't have to be 100% accurate since it's only a preview but I'd like it to be as close as reasonable (without an ajax call to slow things down)

    My function I started with is:

    var urlify = function(input) {
        return input.replace(/[ &]+/gi, '-').toLowerCase();
    };
    

    But & and space aren't the only characters I need to deal with For example for a page named: Ice Cream & Soufflé Stored at 4°C this function predicts the URL will be: http://localhost:24918/ice-cream-soufflé-stored-at-4°c/

    I could add ° to my regex but I'm sure there are more characters. In v7 there used to be a list of characters in umbracoSettings.config but it's not there anymore.

    Cheers.
    Murray.

  • Nik 1591 posts 7148 karma points MVP 6x c-trib
    May 07, 2020 @ 07:41
    Nik
    0

    Hi Murry,

    Do you mean this list here?

      <char org=" ">-</char>
      <char org="!"></char>
      <char org="#"></char>
      <char org="%">percent</char>
      <char org="&amp;">and</char>
      <char org="&gt;"></char>
      <char org="&lt;"></char>
      <char org="&quot;"></char>
      <char org="'"></char>
      <char org="("></char>
      <char org=")"></char>
      <char org="*">star</char>
      <char org="+">plus</char>
      <char org=","></char>
      <char org="."></char>
      <char org="/"></char>
      <char org=":"></char>
      <char org=";"></char>
      <char org="="></char>
      <char org="?"></char>
      <char org="@">at</char>
      <char org="["></char>
      <char org="]"></char>
      <char org="^"></char>
      <char org="_"></char>
      <char org="`"></char>
      <char org="{"></char>
      <char org="}"></char>
      <char org="¦"></char>
      <char org="¬"></char>
      <char org="ß">ss</char>
      <char org="ä">ae</char>
      <char org="Ä">ae</char>
      <char org="å">aa</char>
      <char org="æ">ae</char>
      <char org="ö">oe</char>
      <char org="Ö">oe</char>
      <char org="ø">oe</char>
      <char org="ü">ue</char>
      <char org="-">-</char>
      <char org="'"></char>
      <char org="`"></char>
      <char org="'"></char>
      <char org="$">USD</char>
      <char org="£">GBP</char>
      <char org="?">EUR</char>
    

    If so, here is a copy of it, but you can add it back into the umbracoSettings.config file in v8 as it's still valid as part of the configuration.

    As for a JS function to replicate it, I'm not sure I'm affraid.

    Nik

Please Sign in or register to post replies

Write your reply to:

Draft