Skip to content

Instantly share code, notes, and snippets.

@rauschma
Last active February 5, 2024 04:12
Show Gist options
  • Select an option

  • Save rauschma/c46fc10f671ed5bf14021bc14f101c8d to your computer and use it in GitHub Desktop.

Select an option

Save rauschma/c46fc10f671ed5bf14021bc14f101c8d to your computer and use it in GitHub Desktop.

Strings

String.prototype.*

  • charAt(pos: number): string ES1

    Returns the character at index pos, as a string (JavaScript does not have a datatype for characters). str[i] is equivalent to str.charAt(i) and more concise.

    > 'abc'.charAt(1)
    'b'
    
  • charCodeAt(pos: number): number ES1

    Returns the 16-bit number (0–65535) of the UTF-16 code unit (character) at index pos.

    > 'abc'.charCodeAt(1)
    98
    
  • codePointAt(pos: number): number | undefined ES6

    Returns the number of the Unicode code point of the 1–2 characters at index pos. If there is no such index, it returns undefined.

  • concat(...strings: string[]): string ES3

    Returns the concatenation of this and strings. 'a'+'b' is equivalent to 'a'.concat('b') and more concise.

    > 'ab'.concat('cd', 'ef', 'gh')
    'abcdefgh'
    
  • endsWith(searchString: string, endPos=this.length): boolean ES6

    Returns true if this ends with searchString at index endPos and false, otherwise.

    > 'foo.txt'.endsWith('.txt')
    true
    > 'abc'.endsWith('ab', 2)
    true
    
  • includes(searchString: string, startPos=0): boolean ES6

    Returns true if this contains the searchString and false, otherwise. The search starts at startPos.

    > 'abc'.includes('b')
    true
    > 'abc'.includes('b', 2)
    false
    
  • indexOf(searchString: string, minIndex=0): number ES1

    Returns the lowest index at which searchString appears within this, or -1, otherwise. Any returned index will be minIndex or higher.

    > 'abab'.indexOf('a')
    0
    > 'abab'.indexOf('a', 1)
    2
    > 'abab'.indexOf('c')
    -1
    
  • lastIndexOf(searchString: string, maxIndex=Infinity): number ES1

    Returns the highest index at which searchString appears within this, or -1, otherwise. Any returned index will be maxIndex or lower.

    > 'abab'.lastIndexOf('ab', 2)
    2
    > 'abab'.lastIndexOf('ab', 1)
    0
    > 'abab'.lastIndexOf('ab')
    2
    
  • match(regExp: string | RegExp): RegExpMatchArray | null ES3

    If regExp is a regular expression with flag /g not set then .match() returns the first match for regExp within this. Or null if there is no match. If regExp is a string, it is converted to a regular expression before performing the previous steps.

    The result has the following type:

    interface RegExpMatchArray extends Array<string> {
      index: number;
      input: string;
      groups: undefined | {
        [key: string]: string
      };
    }

    Numbered capture groups become Array indices. Named capture groups (ES2018) become properties of .groups. In this mode, .match() works like RegExp.prototype.exec().

    Examples:

    > 'ababb'.match(/a(b+)/)
    [ 'ab', 'b', index: 0, input: 'ababb', groups: undefined ]
    > 'ababb'.match(/a(?<foo>b+)/)
    [ 'ab', 'b', index: 0, input: 'ababb', groups: { foo: 'b' } ]
    > 'abab'.match(/x/)
    null
    
  • match(regExp: RegExp): string[] | null ES3

    If flag /g of regExp is set, .match() returns either an Array with all matches or null if there was no match.

    > 'ababb'.match(/a(b+)/g)
    [ 'ab', 'abb' ]
    > 'ababb'.match(/a(?<foo>b+)/g)
    [ 'ab', 'abb' ]
    > 'abab'.match(/x/g)
    null
    
  • normalize(form: 'NFC'|'NFD'|'NFKC'|'NFKD' = 'NFC'): string;

    Normalizes this according to the Unicode Normalization Forms.

TODO

  • String.prototype.padEnd( maxLength [ , fillString ] )
  • String.prototype.padStart( maxLength [ , fillString ] )
  • String.prototype.repeat ( count )
  • String.prototype.replace ( searchValue, replaceValue )
  • String.prototype.search ( regexp )
  • String.prototype.slice ( start, end )
  • String.prototype.split ( separator, limit )
  • String.prototype.startsWith ( searchString [ , position ] )
  • String.prototype.substring ( start, end )
  • String.prototype.toLocaleLowerCase ( [ reserved1 [ , reserved2 ] ] )
  • String.prototype.toLocaleUpperCase ( [ reserved1 [ , reserved2 ] ] )
  • String.prototype.toLowerCase ( )
  • String.prototype.toString ( )
  • String.prototype.toUpperCase ( )
  • String.prototype.trim ( )
  • String.prototype.valueOf ( )
  • String.prototype [ @@iterator ] ( )

More information

Sources

@ashu17706
Copy link

ashu17706 commented Apr 17, 2018

So to replace all '.'

> 'x.x.'.replace('.', '#')
'x#x.'
> 'x.x.'.replace(/./, '#')
'#.x.'
> 'x.x.'.replace(/./g, '#')
'x#x#'

we need to skip the special character like this.

> 'x.x.'.replace(/\./g, '#')
'x#x#'

@r0mflip
Copy link

r0mflip commented Apr 17, 2018

Can the startsWith example

> 'abc'.startsWith('bc', 1)
true

be

> 'abcdef'.startsWith('bc', 1)
true

so to be more intuitive, first example took a bit of time to understand.

And can you please add an example for split with the limit argument, and add links to MDN and the spec.

@liuamy840
Copy link

liuamy840 commented Jun 1, 2018

Hello. I have tried 'ababb'.match(/a(?<foo>b+)/) in node environment. It returns SyntaxError: Invalid regular expression: /a(?<foo>b+)/: Invalid group. How to make it work?

@g-koziol
Copy link

g-koziol commented Jun 2, 2018

@liuamy840 Node.js have support for named groups in specyfic versions by default or requires the --harmony flag.
You can check this on:
RegExp named groups support in Node

@dwhieb
Copy link

dwhieb commented Jul 24, 2018

This is really nice! You should consider these explanations and examples to MDN's documentation on String methods: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment