JavaScript

StringextractFragment Method

Syntax

String.extractFragment(instance[,separator])

Arguments

instancenumber

The zero base index of the fragment to extract.

separatorstringregexparray

The string or regular expression to use as the boundary of the fragments. An array of two strings and/or regular expressions can be passed in to define a separate start and end boundary.

Returns

fragmentstring

A fragment of the string.

Description

Extension to the native string variable to support extraction of string fragments.

Discussion

The String.extractFragment method can be used to extract fragments of text from a string.

The "instance" argument is the index of the fragment to return.

The optional "separator" argument is the string(s) and/or regular expression(s) to use as the boundaries of the fragments. By default the separators are white-space and punctuation characters.

Example

var str = 'hello world (the earth)';
var frag = str.extractFragment(1);
// frag = 'world'
frag = str.extractFragment(0,['(',')']);
// frag = 'the earth'