Journey4tech
Journey4tech

Reputation: 51

hindi font slug convert

<script type="text/javascript">

    $("#title").keyup(function () {

      var str = $(this).val();
      var trimmed=$.trim(str)
      var slug=trimmed.replace(/[^a-z0-9-]/gi, '-').
       replace(/-+/g, '-').
    replace(/^-|-$/g, '');
    var check =slug.toLowerCase();

    $("#slug").val(slug.toLowerCase());


    });

</script>

I am able to convert english alphanumberic . BUt not able to convert hindi fonts like :

अत्याधुनिक प्रविधि भित्र्याइँदै

Upvotes: 3

Views: 678

Answers (2)

Journey4tech
Journey4tech

Reputation: 51

Now its work for me . I can convert Hindi and English text to slug.

<script type="text/javascript">
$("#title").keyup(function () {
    var str = $(this).val();
    str.replace(/[`~!@#$%^&*()_\-+=\[\]{};:'"\\|\/,.<>?\s]/g, ' ').toLowerCase();
    str.replace(/^\s+|\s+$/gm,'');
    var slug=str.replace(/\s+/g, '-');
    var trimmed=$.trim(str)
    var check =slug.toLowerCase();
    $("#slug").val(slug.toLowerCase());
});
</script>

Upvotes: 0

ᴄʀᴏᴢᴇᴛ
ᴄʀᴏᴢᴇᴛ

Reputation: 2999

Your function works as intended :

  • You replace all non alphanumeric char with hyphen. your example string will look like -------------
  • then you remove the duplicated hyphens, this will give you -.
  • finally, you trim the hyphens that are on begining or end of the string. this will give you an empty string.

if you try your function on this string : अत्याधुनिक aze aze प्रविधि भित्र्याइँदै you will get aze-aze.

Perhaps you should have a look at transliteration libraries such as https://github.com/andyhu/transliteration This lib provides a function slugify that you can use on your hindi string to get an alphanumeric string.

slugify('अत्याधुनिक प्रविधि भित्र्याइँदै') will give "atyaadhunik-prvidhi-bhitryaaindai"

Upvotes: 3

Related Questions