• 5 String Manipulation Libraries for JavaScript
  • By Mike Chen
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: jaredliw
  • Proofreader: KimYangOfCat

Five JavaScript character manipulation libraries

Working with strings can be a tedious task because we need to consider many different use cases. For example, something as simple as turning a string into a hump format can require several lines of code.

function camelize(str) {
  return str.replace(/ (? :^\w|[A-Z]|\b\w|\s+)/g.function(match, index) {
    if (+match === 0) return ""; // or if (/\s+/.test(match)) to match whitespace characters
    return index === 0 ? match.toLowerCase() : match.toUpperCase();
  });
}
Copy the code

The code snippet above is the most well-received answer on Stack Overflow. However, it does not solve the use case where a string contains –Foo– bAr–.

This is where the string handling library comes in. These libraries consider every possible use case for a given problem, making it easy to implement complex string operations. This helps you because you only need to call a function to get a valid solution.

Let’s take a look at a couple of string handling libraries in a couple of JavaScript.

1. String.js

String.js (S for short) is a lightweight (less than 5 kB compressed) JavaScript library that provides additional String manipulation methods for browsers or Node.js.

installation

npm i string
Copy the code

Noteworthy method

  • between(left, right)Extract –leftrightAll characters between strings.

This method can be used to extract elements between HTML tags.

var S = require('string');
S('<a>This is a link</a>').between('<a>'.'</a>').s 
// => 'This is a link'
Copy the code
  • camelize()— Remove all underscores and dashes, and convert the string to hump format.

This method can be used to solve the problem at the beginning of this article.

var S = require('string');
S('---Foo---bAr---').camelize().s; 
// => 'fooBar'
Copy the code
  • humanize()— Turn input into a human form.

Implementing this function from scratch must require quite a few lines of code.

var S = require('string');
S(' capitalize dash-CamelCase_underscore trim ').humanize().s
// => 'Capitalize dash camel case underscore trim'
Copy the code
  • stripPunctuation()— Removes all punctuation from the given string.

If you implement this function from scratch, you will probably miss some punctuation.

var S = require('string');
S('My, st[ring] *full* of %punct)').stripPunctuation().s; 
// => 'My string full of punct'
Copy the code

You can check out more methods here.

2. Voca

Voca is a JavaScript string manipulation library. This library contains changing case, trimming, filling, generating slug, Latinizing, sprintf formatting, truncation, escaping and other useful string manipulation methods. To reduce application builds, Voca’s modular build allows you to load only specific features. The library is thoroughly tested, well documented, and provides long-term support.

installation

npm i voca
Copy the code

Noteworthy method

  • camelCase(String data)

Convert data to hump format.

var v = require('voca');
v.camelCase('foo Bar');
// => 'fooBar'

v.camelCase('FooBar');
// => 'fooBar'

v.camelCase('---Foo---bAr---');
// => 'fooBar'
Copy the code
  • latinise(String data)

Latinize data by removing diacritics.

var v = require('voca');
v.latinise('cafe\u0301'); / / / 'cafe'
// => 'cafe'

v.latinise('produce aout decembre');
// => 'aout decembre'

v.latinise('seem а seem п р е seem р а с е н э т о т м и р');
// => 'kak prekrasen etot mir'
Copy the code
  • isAlphaDigit(String data)

Check whether data contains only alphanumeric characters (literal digit strings).

var v = require('voca');
v.isAlphaDigit('year2020');
// => true

v.isAlphaDigit('1448');
// => true

v.isAlphaDigit('40-20');
// => false
Copy the code
  • countWords(String data)

Count the number of words in data.

var v = require('voca');
v.countWords('gravity can cross dimensions');
/ / = > 4

v.countWords('GravityCanCrossDimensions');
/ / = > 4

v.countWords('Gravity - can cross dimensions! ');
/ / = > 4
Copy the code
  • escapeRegExp(String data)

Escape special characters in regular expressions — – [] / {} () * +? . $| \ ^.

var v = require('voca');
v.escapeRegExp('(hours)[minutes]{seconds}');
// => '(hours)[minutes]{seconds}'
Copy the code

You can view more information here.

3. Anchorme.js

This is a small and fast JavaScript library. It helps you detect links, urls, email addresses, etc., and turn them into clickable HTML anchor links.

  • High sensitivity, low false positive rate.
  • Verify urls and email addresses against a complete IANA list.
  • Verify the port number, if any.
  • Verify the IP address, if any.
  • Urls that can detect non-Latin letters.

installation

npm i anchorme
Copy the code

usage

import anchorme from "anchorme"; 
/ / or
// var anchorme = require("anchorme").default;

const input = "some text with a link.com"; 
const resultA = anchorme(input);
// => 'some text with a <a href="http://link.com">link.com</a>'
Copy the code

You can pass in additional extensions to further customize this functionality.

4. Underscore.string

Underscore. String is a JavaScript string manipulation extension that you can use in conjunction with Underscore. Underscore. String is an extension of Underscore. Js, inspired by prototype. js, right.js, and Underscore. This JavaScript library lets you manipulate strings with ease.

Underscore. String provides several useful functions for you, such as: Capitalize, Clean, includes, Count, escapeHTML, unescapeHTML, INSERT, splice, startsWith, endsWith, Titleize, trim, truncate, etc.

installation

npm install underscore.string
Copy the code

Noteworthy method

  • numberFormat(number)— Format numbers.

Formats a number as a string with a decimal point and a ten thousand separator.

var _ = require("underscore.string");

_.numberFormat(1000.3)
// => "1,000.000"

_.numberFormat(123456789.123.5.'. '.', ');
/ / = > "123456789123"
Copy the code
  • levenshtein(string1, string2)Calculate the Levenstein distance between two strings.

You can learn more about Levenstein’s distance algorithm here.

var _ = require("underscore.string");

_.levenshtein('kitten'.'kittah');
/ / = > 2
Copy the code
  • chop(string, step)— Cuts the specified string into multiple segments.
_.chop('whitespace'.3);
// => ['whi','tes','pac','e']
Copy the code

You can learn more about Underscore String here.

5. Stringz

The main attraction of this library is its Ability to recognize Unicode. If you run the following code, the output will be 2.

"🤔".length;
/ / = > 2
Copy the code

This is because string.length () returns the number of code units in the String, not the number of characters. In practice, characters in the range 010000 to 03FFFF and 040000 to 10FFFF require 4 bytes (32 bits) and 1 code point to represent. It makes no difference to the result. However, some characters require more than two bytes to represent, and they require more than one code point at a time.

You can read more about JavaScript Unicode issues here.

installation

npm install stringz
Copy the code

Noteworthy method

  • limit(string, limit, padString, padPosition)

Limits the string length to a given length.

const stringz = require('stringz');

/ / truncation.
stringz.limit('Life’s like a box of chocolates.'.20); 
// => "Life's like a box of"

/ / fill:
stringz.limit('Everybody loves emojis! '.26.'💩'); 
// => "Everybody loves emojis! 💩 💩 💩"
stringz.limit('What are you looking at? '.30.'+'.'left'); 
// => "++++++What are you looking at?"

// Unicode is recognized
stringz.limit('🤔 🤔 🤔'.2); 
/ / = > "🤔 🤔"
stringz.limit('👍 🏽 👍 🏽'.4.'👍 🏽'); 
/ / = > "👍 🏽 👍 🏽 👍 🏽 👍 🏽"
Copy the code
  • toArray(string)

Convert a string to an array:

const stringz = require('stringz');

stringz.toArray('abc');
// ['a','b','c']

// Unicode is recognized
stringz.toArray('👍 🏽 🍆 🌮');
/ / / '👍 🏽', '🍆', '🌮]
Copy the code

For more information about Stringz, visit the Github repository for Stringz.

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.