Nodejs String Split splitOnce(delimiter)

Here you can find the source of splitOnce(delimiter)

Method Source Code

String.prototype.splitOnce = function (delimiter) {
    var components = this.split(delimiter);
    return [components.shift(), components.join(delimiter)];
};
if (!String.prototype.endsWith) {
  String.prototype.endsWith = function(searchString, position) {
      var subjectString = this.toString();
      if (position === undefined || position > subjectString.length) {
          position = subjectString.length;
      }//from ww  w  .  j  a v a2 s  . c  om
      position -= searchString.length;
      var lastIndex = subjectString.indexOf(searchString, position);
      return lastIndex !== -1 && lastIndex === position;
  };
}
if (!String.prototype.trim) {
    String.prototype.trim = function () {
        return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
    };
}

Related

  1. splitFirst(separator)
    String.prototype.splitFirst = function (separator) {
        var si = this.split(separator);
        var output = [];
        output.push(si[0]);
        if (si.length>1) {
            si.shift();
            output.push(si.join(separator));
        return output;
    ...
    
  2. splitFirst(x)
    String.prototype.splitFirst = function(x){
      var index = this.indexOf(x);
      return [
        this.substring(0, index),
        this.substring(index + x.length)
      ]
    };
    
  3. splitLast(separator)
    String.prototype.splitLast = function(separator) {
        var si = this.split(separator);
        var output = [];
        var last = si.pop();
        if (si.length > 0) {
            output.push(si.join(separator));
        if (last) {
            output.push(last);
    ...
    
  4. splitLength(size)
    String.prototype.splitLength = function(size) {
        var regex = new RegExp('.{1,' + size + '}', 'g');
        return this.match(regex);
    };
    
  5. splitLines(splitOptions = removeEmptyEntries: true, trim: true })
    String.prototype.splitLines = function (splitOptions = { removeEmptyEntries: true, trim: true }) {
        let lines = this.split(/\r?\n/g);
        if (splitOptions && splitOptions.trim) {
            lines = lines.filter(line => line.trim());
        if (splitOptions && splitOptions.removeEmptyEntries) {
            lines = lines.filter(line => line.length > 0);
        return lines;
    ...
    
  6. splitOnce(regex)
    String.prototype.splitOnce = function (regex) {
        var match = this.match(regex);
        if (match) {
            var match_i = this.indexOf(match[0]);
            return [this.substring(0, match_i),
            this.substring(match_i + match[0].length)];
        } else {
            return [this, ""];
    
  7. splitStr(delimiter)
    function reverseWords(str) {
      var result = "";
      var wordArray = str.split(" ");
      for (var i = wordArray.length - 1; i >= 0; i--) {
        result += wordArray[i] + " ";
      return result.trim(); 
    String.prototype.splitStr = function (delimiter) {
    ...
    
  8. splitWord()
    String.prototype.splitWord = function () {
      return this.split(/(?=[A-Z])/).join(' ');
    };
    
  9. splitchunks(s)
    String.prototype.splitchunks = function(s) {
      return s.split(/\n\n+/)