question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to remove `<` and `>` characters

See original GitHub issue

Thanks for maintaining slugify.

I want to strip < and > characters from strings, but for some reason they always convert to less and greater respectively. Is there a way to do this?

Here’s my input, expected result, and actual result:

slugify('Hello <world>', {
    replacement: '',
    remove: /[=_'\-+,<>.()[\]]/g,
    lower: true,
    strict: true
  })

// expected: `helloworld`
// actual: `hellolessworldgreater`

Here is my workaround:

slugify('Hello <world>'.replace(/[<>]/g, ''), {
    replacement: '',
    remove: /[=_'\-+,<>.()[\]]/g,
    lower: true,
    strict: true
  })

// output: `helloworld`

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Trottcommented, Feb 16, 2022

@theetrain In that case, you might want to use slug instead of slugify. Assuming you want only Latin alphabet characters, by default slug will only produce those. slugify will preserve many special characters. Given the input 'We Got Symbols Like ~!@*()_+', slugify will output 'We-Got-Symbols-Like-~!@*()_+' by default, while slug will output 'we-got-symbols-like' by default.

I also probably wouldn’t use two slug rules. Unless there’s an important reason, I’d suggest using slug() to create the user handle, and then using String.prototype.replaceAll() create the email handle.

const slug = require('slug')

function makeHandles(name) {
  const userHandle = slug(name)
  const emailHandle = userHandle.replaceAll('-', '')
  return ({ userHandle, emailHandle })
}

console.log(makeHandles('John Doe'))
console.log(makeHandles('Super Company©'))
console.log(makeHandles('Super Company™'))
console.log(makeHandles('We Got Symbols Like ~!@*()_+'))

Output:

{ userHandle: 'john-doe', emailHandle: 'johndoe' }
{ userHandle: 'super-company', emailHandle: 'supercompany' }
{ userHandle: 'super-company', emailHandle: 'supercompany' }
{ userHandle: 'we-got-symbols-like', emailHandle: 'wegotsymbolslike' }
0reactions
Trottcommented, Feb 16, 2022

(Closing because I think you have your answer, but if not, feel free to re-open and/or comment.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Remove Characters from a String in Python
Remove Newline Characters From a String Using the replace() Method ... Declare a string variable with some newline characters: s = 'ab\ncd\nef'.
Read more >
5 Different Ways to Remove Specific Characters From a String ...
1. Remove Specific Characters From the String. Using 'str. · 2. Remove All Characters Except Alphabets From a String. Using 'isalpha()' · 3....
Read more >
5 Ways to Remove a Character from String in Python
1. Removing a Character from String using the Naive method · 2. Removal of Character from a String using replace() Method · 3....
Read more >
How to remove characters/text from string in Excel - Ablebits
To remove a particular character(s) from multiple cells at once, select Remove custom characters. As an example, we are deleting all occurrences ...
Read more >
Remove characters from the first string which are present in ...
Remove characters from the first string which are present in the second string ; Input: string1 = “occurrence” string2 = “car” ; Output:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found