JavaScript trim function

A JavaScript equivalent of PHP’s trim

function trim (str, charlist) {
  // http://kevin.vanzonneveld.net
  // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   improved by: mdsjack (http://www.mdsjack.bo.it)
  // +   improved by: Alexander Ermolaev (http://snippets.dzone.com/user/AlexanderErmolaev)
  // +      input by: Erkekjetter
  // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +      input by: DxGx
  // +   improved by: Steven Levithan (http://blog.stevenlevithan.com)
  // +    tweaked by: Jack
  // +   bugfixed by: Onno Marsman
  // *     example 1: trim('    Kevin van Zonneveld    ');
  // *     returns 1: 'Kevin van Zonneveld'
  // *     example 2: trim('Hello World', 'Hdle');
  // *     returns 2: 'o Wor'
  // *     example 3: trim(16, 1);
  // *     returns 3: 6
  var whitespace, l = 0,
    i = 0;
  str += '';

  if (!charlist) {
    // default list
    whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
  } else {
    // preg_quote custom list
    charlist += '';
    whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
  }

  l = str.length;
  for (i = 0; i < l; i++) {
    if (whitespace.indexOf(str.charAt(i)) === -1) {
      str = str.substring(i);
      break;
    }
  }

  l = str.length;
  for (i = l - 1; i >= 0; i--) {
    if (whitespace.indexOf(str.charAt(i)) === -1) {
      str = str.substring(0, i + 1);
      break;
    }
  }

  return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

Example 1
trim('    Kevin van Zonneveld    ');

Should return
'Kevin van Zonneveld'

Example 2
trim('Hello World', 'Hdle');

Should return
'o Wor'

Example 3
trim(16, 1);

Should return
6

Добавлено: 27 Июля 2018 22:04:12 Добавил: Андрей Ковальчук

JavaScript substr_replace function

A JavaScript equivalent of PHP’s substr_replace

function substr_replace (str, replace, start, length) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: substr_replace('ABCDEFGH:/MNRPQR/', 'bob', 0);
  // *     returns 1: 'bob'
  // *     example 2: $var = 'ABCDEFGH:/MNRPQR/';
  // *     example 2: substr_replace($var, 'bob', 0, $var.length);
  // *     returns 2: 'bob'
  // *     example 3: substr_replace('ABCDEFGH:/MNRPQR/', 'bob', 0, 0);
  // *     returns 3: 'bobABCDEFGH:/MNRPQR/'
  // *     example 4: substr_replace('ABCDEFGH:/MNRPQR/', 'bob', 10, -1);
  // *     returns 4: 'ABCDEFGH:/bob/'
  // *     example 5: substr_replace('ABCDEFGH:/MNRPQR/', 'bob', -7, -1);
  // *     returns 5: 'ABCDEFGH:/bob/'
  // *     example 6: 'substr_replace('ABCDEFGH:/MNRPQR/', '', 10, -1)'
  // *     returns 6: 'ABCDEFGH://'
  if (start < 0) { // start position in str
    start = start + str.length;
  }
  length = length !== undefined ? length : str.length;
  if (length < 0) {
    length = length + str.length - start;
  }
  return str.slice(0, start) + replace.substr(0, length) + replace.slice(length) + str.slice(start + length);
}

Example 1
substr_replace('ABCDEFGH:/MNRPQR/', 'bob', 0);

Should return
'bob'

Example 2
$var = 'ABCDEFGH:/MNRPQR/';
substr_replace($var, 'bob', 0, $var.length);

Should return
'bob'

Example 3
substr_replace('ABCDEFGH:/MNRPQR/', 'bob', 0, 0);

Should return
'bobABCDEFGH:/MNRPQR/'

Добавлено: 27 Июля 2018 22:03:03 Добавил: Андрей Ковальчук

JavaScript substr_count function

A JavaScript equivalent of PHP’s substr_count

function substr_count (haystack, needle, offset, length) {
  // http://kevin.vanzonneveld.net
  // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   bugfixed by: Onno Marsman
  // +   improved by: Brett Zamir (http://brett-zamir.me)
  // +   improved by: Thomas
  // *     example 1: substr_count('Kevin van Zonneveld', 'e');
  // *     returns 1: 3
  // *     example 2: substr_count('Kevin van Zonneveld', 'K', 1);
  // *     returns 2: 0
  // *     example 3: substr_count('Kevin van Zonneveld', 'Z', 0, 10);
  // *     returns 3: false

  var cnt = 0;

  haystack += '';
  needle += '';
  if (isNaN(offset)) {
    offset = 0;
  }
  if (isNaN(length)) {
    length = 0;
  }
  if (needle.length == 0) {
    return false;
  }
  offset--;

  while ((offset = haystack.indexOf(needle, offset + 1)) != -1) {
    if (length > 0 && (offset + needle.length) > length) {
      return false;
    }
    cnt++;
  }

  return cnt;
}

Example 1
substr_count('Kevin van Zonneveld', 'e');

Should return
3

Example 2
substr_count('Kevin van Zonneveld', 'K', 1);

Should return
0

Example 3
substr_count('Kevin van Zonneveld', 'Z', 0, 10);

Should return
false

Добавлено: 27 Июля 2018 22:01:29 Добавил: Андрей Ковальчук

JavaScript substr_compare function

A JavaScript equivalent of PHP’s substr_compare

function substr_compare (main_str, str, offset, length, case_insensitivity) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // +   derived from: strcasecmp, strcmp
  // *     example 1: substr_compare("abcde", "bc", 1, 2);
  // *     returns 1: 0
  if (!offset && offset !== 0) {
    throw 'Missing offset for substr_compare()';
  }

  if (offset < 0) {
    offset = main_str.length + offset;
  }

  if (length && length > (main_str.length - offset)) {
    return false;
  }
  length = length || main_str.length - offset;

  main_str = main_str.substr(offset, length);
  str = str.substr(0, length); // Should only compare up to the desired length
  if (case_insensitivity) { // Works as strcasecmp
    main_str = (main_str + '').toLowerCase();
    str = (str + '').toLowerCase();
    if (main_str == str) {
      return 0;
    }
    return (main_str > str) ? 1 : -1;
  }
  // Works as strcmp
  return ((main_str == str) ? 0 : ((main_str > str) ? 1 : -1));
}

Example 1
substr_compare("abcde", "bc", 1, 2);

Should return
0

Добавлено: 27 Июля 2018 21:53:24 Добавил: Андрей Ковальчук

JavaScript strrpos function

A JavaScript equivalent of PHP’s strrpos

function strrpos (haystack, needle, offset) {
  // http://kevin.vanzonneveld.net
  // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   bugfixed by: Onno Marsman
  // +   input by: saulius
  // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: strrpos('Kevin van Zonneveld', 'e');
  // *     returns 1: 16
  // *     example 2: strrpos('somepage.com', '.', false);
  // *     returns 2: 8
  // *     example 3: strrpos('baa', 'a', 3);
  // *     returns 3: false
  // *     example 4: strrpos('baa', 'a', 2);
  // *     returns 4: 2
  var i = -1;
  if (offset) {
    i = (haystack + '').slice(offset).lastIndexOf(needle); // strrpos' offset indicates starting point of range till end,
    // while lastIndexOf's optional 2nd argument indicates ending point of range from the beginning
    if (i !== -1) {
      i += offset;
    }
  } else {
    i = (haystack + '').lastIndexOf(needle);
  }
  return i >= 0 ? i : false;
}

Example 1
strrpos('Kevin van Zonneveld', 'e');

Should return
16

Example 2
strrpos('somepage.com', '.', false);

Should return
8

Example 3
strrpos('baa', 'a', 3);

Should return
false

Добавлено: 27 Июля 2018 21:52:18 Добавил: Андрей Ковальчук

JavaScript strtolower function

A JavaScript equivalent of PHP’s strtolower

function strtolower (str) {
  // http://kevin.vanzonneveld.net
  // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   improved by: Onno Marsman
  // *     example 1: strtolower('Kevin van Zonneveld');
  // *     returns 1: 'kevin van zonneveld'
  return (str + '').toLowerCase();
}

Example 1
strtolower('Kevin van Zonneveld');

Should return
'kevin van zonneveld'

Добавлено: 27 Июля 2018 21:51:06 Добавил: Андрей Ковальчук

JavaScript strtok function

A JavaScript equivalent of PHP’s strtok

function strtok (str, tokens) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // %        note 1: Use tab and newline as tokenizing characters as well
  // *     example 1: $string = "\t\t\t\nThis is\tan example\nstring\n";
  // *     example 1: $tok = strtok($string, " \n\t");
  // *     example 1: $b = '';
  // *     example 1: while ($tok !== false) {$b += "Word="+$tok+"\n"; $tok = strtok(" \n\t");}
  // *     example 1: $b
  // *     returns 1: "Word=This\nWord=is\nWord=an\nWord=example\nWord=string\n"
  // BEGIN REDUNDANT
  this.php_js = this.php_js || {};
  // END REDUNDANT
  if (tokens === undefined) {
    tokens = str;
    str = this.php_js.strtokleftOver;
  }
  if (str.length === 0) {
    return false;
  }
  if (tokens.indexOf(str.charAt(0)) !== -1) {
    return this.strtok(str.substr(1), tokens);
  }
  for (var i = 0; i < str.length; i++) {
    if (tokens.indexOf(str.charAt(i)) !== -1) {
      break;
    }
  }
  this.php_js.strtokleftOver = str.substr(i + 1);
  return str.substring(0, i);
}

Example 1
$string = "\t\t\t\nThis is\tan example\nstring\n";
$tok = strtok($string, " \n\t");
$b = '';
while ($tok !== false) {$b += "Word="+$tok+"\n"; $tok = strtok(" \n\t");}
$b

Should return
"Word=This\nWord=is\nWord=an\nWord=example\nWord=string\n"

Добавлено: 27 Июля 2018 21:50:26 Добавил: Андрей Ковальчук

JavaScript strstr function

A JavaScript equivalent of PHP’s strstr

function strstr (haystack, needle, bool) {
  // http://kevin.vanzonneveld.net
  // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   bugfixed by: Onno Marsman
  // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // *     example 1: strstr('Kevin van Zonneveld', 'van');
  // *     returns 1: 'van Zonneveld'
  // *     example 2: strstr('Kevin van Zonneveld', 'van', true);
  // *     returns 2: 'Kevin '
  // *     example 3: strstr('name@example.com', '@');
  // *     returns 3: '@example.com'
  // *     example 4: strstr('name@example.com', '@', true);
  // *     returns 4: 'name'
  var pos = 0;

  haystack += '';
  pos = haystack.indexOf(needle);
  if (pos == -1) {
    return false;
  } else {
    if (bool) {
      return haystack.substr(0, pos);
    } else {
      return haystack.slice(pos);
    }
  }
}

Example 1
strstr('Kevin van Zonneveld', 'van');

Should return
'van Zonneveld'

Example 2
strstr('Kevin van Zonneveld', 'van', true);

Should return
'Kevin '

Example 3
strstr('name@example.com', '@');

Should return
'@example.com'

Добавлено: 27 Июля 2018 21:49:36 Добавил: Андрей Ковальчук

JavaScript strspn function

A JavaScript equivalent of PHP’s strspn

function strspn (str1, str2, start, lgth) {
  // http://kevin.vanzonneveld.net
  // +   original by: Valentina De Rosa
  // +   improved by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: strspn('42 is the answer, what is the question ...', '1234567890');
  // *     returns 1: 2
  // *     example 2: strspn('foo', 'o', 1, 2);
  // *     returns 2: 2
  var found;
  var stri;
  var strj;
  var j = 0;
  var i = 0;

  start = start ? (start < 0 ? (str1.length + start) : start) : 0;
  lgth = lgth ? ((lgth < 0) ? (str1.length + lgth - start) : lgth) : str1.length - start;
  str1 = str1.substr(start, lgth);

  for (i = 0; i < str1.length; i++) {
    found = 0;
    stri = str1.substring(i, i + 1);
    for (j = 0; j <= str2.length; j++) {
      strj = str2.substring(j, j + 1);
      if (stri == strj) {
        found = 1;
        break;
      }
    }
    if (found != 1) {
      return i;
    }
  }

  return i;
}

Example 1
strspn('42 is the answer, what is the question ...', '1234567890');

Should return
2

Example 2
strspn('foo', 'o', 1, 2);

Should return
2

Добавлено: 27 Июля 2018 21:48:26 Добавил: Андрей Ковальчук

JavaScript strripos function

A JavaScript equivalent of PHP’s strripos

function strripos (haystack, needle, offset) {
  // http://kevin.vanzonneveld.net
  // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   bugfixed by: Onno Marsman
  // +   input by: saulius
  // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: strripos('Kevin van Zonneveld', 'E');
  // *     returns 1: 16
  haystack = (haystack + '').toLowerCase();
  needle = (needle + '').toLowerCase();

  var i = -1;
  if (offset) {
    i = (haystack + '').slice(offset).lastIndexOf(needle); // strrpos' offset indicates starting point of range till end,
    // while lastIndexOf's optional 2nd argument indicates ending point of range from the beginning
    if (i !== -1) {
      i += offset;
    }
  } else {
    i = (haystack + '').lastIndexOf(needle);
  }
  return i >= 0 ? i : false;
}

Example 1
strripos('Kevin van Zonneveld', 'E');

Should return
16

Добавлено: 27 Июля 2018 21:47:22 Добавил: Андрей Ковальчук

JavaScript strrev function

A JavaScript equivalent of PHP’s strrev

function strrev (string) {
  // http://kevin.vanzonneveld.net
  // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   bugfixed by: Onno Marsman
  // +   reimplemented by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: strrev('Kevin van Zonneveld');
  // *     returns 1: 'dlevennoZ nav niveK'
  // *     example 2: strrev('a\u0301haB') === 'Baha\u0301'; // combining
  // *     returns 2: true
  // *     example 3: strrev('A\uD87E\uDC04Z') === 'Z\uD87E\uDC04A'; // surrogates
  // *     returns 2: true
  string = string + '';

  // Performance will be enhanced with the next two lines of code commented
  //      out if you don't care about combining characters
  // Keep Unicode combining characters together with the character preceding
  //      them and which they are modifying (as in PHP 6)
  // See http://unicode.org/reports/tr44/#Property_Table (Me+Mn)
  // We also add the low surrogate range at the beginning here so it will be
  //      maintained with its preceding high surrogate
  var grapheme_extend = /(.)([\uDC00-\uDFFF\u0300-\u036F\u0483-\u0489\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u0610-\u061A\u064B-\u065E\u0670\u06D6-\u06DC\u06DE-\u06E4\u06E7\u06E8\u06EA-\u06ED\u0711\u0730-\u074A\u07A6-\u07B0\u07EB-\u07F3\u0901-\u0903\u093C\u093E-\u094D\u0951-\u0954\u0962\u0963\u0981-\u0983\u09BC\u09BE-\u09C4\u09C7\u09C8\u09CB-\u09CD\u09D7\u09E2\u09E3\u0A01-\u0A03\u0A3C\u0A3E-\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A51\u0A70\u0A71\u0A75\u0A81-\u0A83\u0ABC\u0ABE-\u0AC5\u0AC7-\u0AC9\u0ACB-\u0ACD\u0AE2\u0AE3\u0B01-\u0B03\u0B3C\u0B3E-\u0B44\u0B47\u0B48\u0B4B-\u0B4D\u0B56\u0B57\u0B62\u0B63\u0B82\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD7\u0C01-\u0C03\u0C3E-\u0C44\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C62\u0C63\u0C82\u0C83\u0CBC\u0CBE-\u0CC4\u0CC6-\u0CC8\u0CCA-\u0CCD\u0CD5\u0CD6\u0CE2\u0CE3\u0D02\u0D03\u0D3E-\u0D44\u0D46-\u0D48\u0D4A-\u0D4D\u0D57\u0D62\u0D63\u0D82\u0D83\u0DCA\u0DCF-\u0DD4\u0DD6\u0DD8-\u0DDF\u0DF2\u0DF3\u0E31\u0E34-\u0E3A\u0E47-\u0E4E\u0EB1\u0EB4-\u0EB9\u0EBB\u0EBC\u0EC8-\u0ECD\u0F18\u0F19\u0F35\u0F37\u0F39\u0F3E\u0F3F\u0F71-\u0F84\u0F86\u0F87\u0F90-\u0F97\u0F99-\u0FBC\u0FC6\u102B-\u103E\u1056-\u1059\u105E-\u1060\u1062-\u1064\u1067-\u106D\u1071-\u1074\u1082-\u108D\u108F\u135F\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17B6-\u17D3\u17DD\u180B-\u180D\u18A9\u1920-\u192B\u1930-\u193B\u19B0-\u19C0\u19C8\u19C9\u1A17-\u1A1B\u1B00-\u1B04\u1B34-\u1B44\u1B6B-\u1B73\u1B80-\u1B82\u1BA1-\u1BAA\u1C24-\u1C37\u1DC0-\u1DE6\u1DFE\u1DFF\u20D0-\u20F0\u2DE0-\u2DFF\u302A-\u302F\u3099\u309A\uA66F-\uA672\uA67C\uA67D\uA802\uA806\uA80B\uA823-\uA827\uA880\uA881\uA8B4-\uA8C4\uA926-\uA92D\uA947-\uA953\uAA29-\uAA36\uAA43\uAA4C\uAA4D\uFB1E\uFE00-\uFE0F\uFE20-\uFE26]+)/g;
  string = string.replace(grapheme_extend, '$2$1'); // Temporarily reverse
  return string.split('').reverse().join('');
}

Example 1
strrev('Kevin van Zonneveld');

Should return
'dlevennoZ nav niveK'

Example 2
strrev('a\u0301haB') === 'Baha\u0301'; // combining

Should return
true
true

Добавлено: 27 Июля 2018 21:46:41 Добавил: Андрей Ковальчук

JavaScript strpos function

A JavaScript equivalent of PHP’s strpos

function strpos (haystack, needle, offset) {
  // http://kevin.vanzonneveld.net
  // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   improved by: Onno Marsman
  // +   bugfixed by: Daniel Esteban
  // +   improved by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
  // *     returns 1: 14
  var i = (haystack + '').indexOf(needle, (offset || 0));
  return i === -1 ? false : i;
}

Example 1
strpos('Kevin van Zonneveld', 'e', 5);

Should return
14

Добавлено: 27 Июля 2018 21:45:43 Добавил: Андрей Ковальчук

JavaScript strrchr function

A JavaScript equivalent of PHP’s strrchr

function strrchr (haystack, needle) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // +   input by: Jason Wong (http://carrot.org/)
  // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: strrchr("Line 1\nLine 2\nLine 3", 10).substr(1)
  // *     returns 1: 'Line 3'
  var pos = 0;

  if (typeof needle !== 'string') {
    needle = String.fromCharCode(parseInt(needle, 10));
  }
  needle = needle.charAt(0);
  pos = haystack.lastIndexOf(needle);
  if (pos === -1) {
    return false;
  }

  return haystack.substr(pos);
}

Example 1
strrchr("Line 1\nLine 2\nLine 3", 10).substr(1)

Should return
'Line 3'

Добавлено: 27 Июля 2018 21:45:05 Добавил: Андрей Ковальчук

JavaScript strncmp function

A JavaScript equivalent of PHP’s strncmp

function strncmp (str1, str2, lgth) {
  // http://kevin.vanzonneveld.net
  // +      original by: Waldo Malqui Silva
  // +         input by: Steve Hilder
  // +      improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +       revised by: gorthaur
  // + reimplemented by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: strncmp('aaa', 'aab', 2);
  // *     returns 1: 0
  // *     example 2: strncmp('aaa', 'aab', 3 );
  // *     returns 2: -1
  var s1 = (str1 + '').substr(0, lgth);
  var s2 = (str2 + '').substr(0, lgth);

  return ((s1 == s2) ? 0 : ((s1 > s2) ? 1 : -1));
}

Example 1
strncmp('aaa', 'aab', 2);

Should return
0

Example 2
strncmp('aaa', 'aab', 3 );

Should return
-1

Добавлено: 27 Июля 2018 21:44:26 Добавил: Андрей Ковальчук

JavaScript strpbrk function

A JavaScript equivalent of PHP’s strpbrk

function strpbrk (haystack, char_list) {
  // http://kevin.vanzonneveld.net
  // +   original by: Alfonso Jimenez (http://www.alfonsojimenez.com)
  // +   bugfixed by: Onno Marsman
  // +    revised by: Christoph
  // +    improved by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: strpbrk('This is a Simple text.', 'is');
  // *     returns 1: 'is is a Simple text.'
  for (var i = 0, len = haystack.length; i < len; ++i) {
    if (char_list.indexOf(haystack.charAt(i)) >= 0) {
      return haystack.slice(i);
    }
  }
  return false;
}

Example 1
strpbrk('This is a Simple text.', 'is');

Should return
'is is a Simple text.'

Добавлено: 27 Июля 2018 21:42:15 Добавил: Андрей Ковальчук