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
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;
}
};