Design & Download
Your JS Library
in Seconds!!!

Snippet: Wrap & Unwrap Strings

Written by Christopher West (cwest) on August 10, 2016.
Surround a string or remove the surrounding substring.
function wrap(str, opt_wrapper, opt_escape) {
  opt_wrapper = toArray(opt_wrapper);
  opt_escape = toArray(opt_escape);
  var opt_lWrap = (opt_wrapper[0] || '"') + '',
      opt_rWrap = opt_wrapper[1] || opt_lWrap,
      opt_lEscape = v1(opt_escape[0], '') + '',
      opt_rEscape = v1(opt_escape[1], opt_lEscape) + '';
  return opt_lWrap
    + (str + '').replace(
        RegExp(quoteRegExp(opt_lWrap) + '|' + quoteRegExp(opt_rWrap), 'g'),
        function(m) { return m == opt_lWrap ? opt_lEscape : opt_rEscape; }
      )
    + opt_rWrap;
}

function unwrap(str, opt_wrapper, opt_escape) {
  opt_wrapper = toArray(opt_wrapper);
  opt_escape = toArray(opt_escape);
  var opt_lWrap = (opt_wrapper[0] || '"') + '',
      opt_rWrap = (opt_wrapper[1] || opt_lWrap) + '',
      opt_lEscape = v1(opt_escape[0], '') + '',
      opt_rEscape = v1(opt_escape[1], opt_lEscape) + '',
      strRgx = '^' + quoteRegExp(opt_lWrap) + '|(' + quoteRegExp(opt_rWrap) 
             + '$)|(' + quoteRegExp(opt_lEscape) + ')|'
             + quoteRegExp(opt_rEscape);
  return (str + '').replace(RegExp(strRgx, 'g'), function(m, r, l, i) {
    return i && !r && m ? l ? opt_lWrap : opt_rWrap : '';
  });
}

wrap(...) API Documentation

Description

Wraps a string with another string, providing the ability to also escape any instances of the surrounding strings.

Parameters

  1. str {*}:
    The string (value coerced to a string) to be wrapped.
  2. opt_wrapper {Array|string}:
    Optional. Defaults to ['"', '"']. If this is a string it will be converted to an array where the string is both the 1st and 2nd value. The 1st value in the array will prefix the returned string. The 2nd value in the array (or the string found in this parameter) will terminate the returned string.
  3. opt_escape {Array|string}:
    Optional. Defaults to ['"', '"']. If this is a string it will be converted to an array where the string is both the 1st and 2nd value. The 1st value in the array will replace all occurrences of the 1st wrapper from opt_wrapper. The 2nd value in the array (or the string found in this parameter) replace all occurrences of the 2nd wrapper from opt_wrapper.

Returns

Returns the 1st wrapper from opt_wrapper, followed by str (with all occurrences of the wrapper(s) replaced with the corresponding escapes from opt_escape), followed by the 2nd wrapper from opt_wrapper.

Example

console.log(YourJS.wrap(0));  // -> '"0"'
console.log(YourJS.wrap('Hello', "'"));  // -> "'Hello'"
console.log('Hello ' + YourJS.wrap('name', ['{', '}']));  // -> 'Hello {name}'
console.log(YourJS.wrap('He always says, "hey guys".', '"', '""'));  // -> '"He always says, ""hey guys""."'
console.log(YourJS.wrap('Hi {name}.', ['{', '}'], ['\\{', '\\}']));  // -> '{Hi \\{name\\}.}'

unwrap(...) API Documentation

Description

Unwraps a string, removing the surrounding substring and also reverting the escaped substrings.

Parameters

  1. str {*}:
    The string (value coerced to a string) to be unwrapped.
  2. opt_wrapper {Array|string}:
    Optional. Defaults to ['"', '"']. If this is a string it will be converted to an array where the string is both the 1st and 2nd value. The 1st value in the array will be removed from the start of the string. The 2nd value in the array (or the string found in this parameter) will be removed from the end of the string.
  3. opt_escape {Array|string}:
    Optional. Defaults to ['', '']. If this is a string it will be converted to an array where the string is both the 1st and 2nd value. All instances of the 1st value in the array found in str will be replaced with the 1st wrapper from opt_wrapper. All instances of the 2nd value in the array (or the string found in this parameter) found in str will be replaced with the 2nd wrapper from opt_wrapper.

Returns

Returns str with the 1st wrapper from opt_wrapper removed from the beginning and the 2nd wrapper from opt_wrapper removed from the end. All instances of the escaped values will be replaced with the corresponding wrapper.

Example

console.log(YourJS.unwrap(0));  // -> '0'
console.log(YourJS.unwrap("'Hello'", "'"));  // -> "Hello"
console.log('Hello ' + YourJS.unwrap('{name}', ['{', '}']));  // -> 'Hello name'
console.log(YourJS.unwrap('"He always says, ""hey guys""."', '"', '""'));  // -> 'He always says, "hey guys".'
console.log(YourJS.unwrap('{Hi \\{name\\}.}', ['{', '}'], ['\\{', '\\}']));  // -> 'Hi {name}.'