0

I want to create a angular js filter who will remove space with "-" and remove special character like ™ tm , ℠ sm, ® r,© .Below code will remove space with "-" but not remove the special characters

 (function (angular) {
    'use strict';
            angular.module('myApp')
            .filter('removespace', function () {
                return function(input) {
                    if(input) {
                        return input.replace(/\s+/g, '-');
                    }
                };
            });

})(angular);


 Output will be

    LG Stylo™ 2 PLUS >> lg-stylo-2-plus
    ZTE AVID® TRIO >> zte-avid-trio
    ZTE ZMAX® Pro >> zte-zmax-pro
    HTC Desire® 530 >> htc-desire-530
    Galaxy S® 6 edge - Gold - 32GB - Cert. Pre-Owned >> galaxy-s-6-edge-gold-32gb-cert-pre-owned

2 Answers 2

1

Try the following:

return input.replace(/\s+/g, '-').replace(/[^-\w]+/g, '').toLowerCase();

([^-\w] means "everything except - and a word character ([a-zA-Z0-9_]", i.e. the resulting string will only contain a-z, A-Z, 0-9, _, -.)

Sign up to request clarification or add additional context in comments.

2 Comments

but it is fail if we convert "Galaxy S® 6 edge - Gold - 32GB - Cert. Pre-Owned".it should be 'galaxy-s-6-edge-gold-32gb-cert-pre-owned' instead of 'galaxy-s-6-edge---gold---32gb---cert-pre-owned'
That's slightly another requirement. Use this: input.replace(/\s+/g, '-').replace(/[^-\w]+/g, '').replace(/-{2,}/g, '-').toLowerCase()
0
input.toLowerCase()
    .replace(/[^\w ]+/g,'')
    .replace(/ +/g,'-');

2 Comments

Not working for this, Galaxy S® 6 edge - Gold - 32GB - Cert. Pre-Owned >> galaxy-s-6-edge-gold-32gb-cert-pre-owned @damian
What about replace(/\s+/g, '-') instead of replace(/ +/g,'-')?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.