Regex to match a string with specific start/end

后端 未结 3 1113
深忆病人
深忆病人 2021-01-25 09:57

I\'m trying to match all occurrences of strings starting with -- and ending with a single space .

The file I\'m handling is the OpenVPN manual

3条回答
  •  轮回少年
    2021-01-25 10:12

    One thing you can do is add these to .prototype and create your own startsWith() and endsWith() functions

    this way you can do string.startsWith("starts with this"); and string.endsWith("ends with this"); I'm using substring instead of indexOf because it's quicker without scanning the entire string. Also, if you pass in an empty string to the functions they return false. Reference link Here

        if ( typeof String.prototype.startsWith != 'function' ) {
      String.prototype.startsWith = function( str ) {
        return str.length > 0 && this.substring( 0, str.length ) === str;
      }
    };
    
    if ( typeof String.prototype.endsWith != 'function' ) {
      String.prototype.endsWith = function( str ) {
        return str.length > 0 && this.substring( this.length - str.length, this.length ) === str;
      }
    };
    

提交回复
热议问题