Jesvin Jose
Jesvin Jose

Reputation: 23098

Language agnostic slug in 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.

Upvotes: 3

Views: 299

Answers (2)

Jimish Fotariya
Jimish Fotariya

Reputation: 1097

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 );

Upvotes: 1

Martin
Martin

Reputation: 16302

This should be simple enough to do with String.prototype.replace() or with regular expressions. Did you try:

'കവാടം പൊളിക്കണം'.replace(' ', '-')

It returns "കവാടം-പൊളിക്കണം".

Upvotes: 0

Related Questions