Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Language agnostic slug in Javascript

Tags:

javascript

I tried slugify and slug packages but strings like കവാടം പൊളിക്കണം: "ലോ അക്കാദമിക്കു" നോട്ടിസ് return a blank string instead of കവാടം-പൊളിക്കണം-ലോ-അക്കാദമിക്കു-നോട്ടിസ്. Words should be preserved, special characters should be stripped.

Is there a way to generate slugs that preserves arbitrary non-English words?


Regular expression to match non-English characters? seems the best resource for matching international words.

like image 947
Jesvin Jose Avatar asked Sep 17 '26 12:09

Jesvin Jose


1 Answers

This following regEx match-replace worked fine for spaces only.

var s    = 'കവാടം പൊളിക്കണം: "ലോ അക്കാദമിക്കു" നോട്ടിസ്';
var res  = s.replace(/ /g,"-");
console.log( res );

For all special characters you can use following.

var s    = 'കവാടം പൊളിക്കണം: "ലോ അക്കാദമിക്കു" നോട്ടിസ്';

// Keep symbols you want to replace with hyphen.
var res  = s.replace(/[&\/\\#,+()$~%.'":*?<>{} ]/g,"-");
console.log( res );
like image 154
Jimish Fotariya Avatar answered Sep 19 '26 03:09

Jimish Fotariya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!