Skip to content

Latest commit

 

History

History
50 lines (33 loc) · 1.07 KB

stringExtras.md

File metadata and controls

50 lines (33 loc) · 1.07 KB

stringExtras

escapeRegExp - Escapes a string to safely be included in regular expressions

import { escapeRegExp } from 'src/string';

const str = 'cat file.js | grep -c';

const result = escapeRegExp(str);

result === 'cat file\\.js \\| grep -c'; // true

escapeXml - Escapes XML (or HTML) content in a string

import { escapeXml } from 'src/string';

const badCode = "<script>alert('hi')</script>";

const sanitized = escapeXml(badCode);

sanitized === '&lt;script&gt;alert(&#39;hi&#39;)&lt;/script&gt;'; // true

padEnd - Adds padding to the end of a string to ensure it is a certain length

import { padEnd } from 'src/string';

const str = 'string';
const length = 10;
const char = '=';

const result = padEnd(str, length, char);

result === 'string===='; // true

padStart - Adds padding to the beginning of a string to ensure it is a certain length

import { padStart } from 'src/string';

const str = 'string';
const length = 10;
const char = '=';

const result = padStart(str, length, char);

result === '====string'; // true