README.md (5935B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2021 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # substringBeforeLast 22 23 > Return the part of a string before the last occurrence of a specified substring. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var substringBeforeLast = require( '@stdlib/string/substring-before-last' ); 41 ``` 42 43 #### substringBeforeLast( str, search ) 44 45 Returns the part of a string before the last occurrence of a specified substring. 46 47 ```javascript 48 var str = 'Beep Boop Beep'; 49 var out = substringBeforeLast( str, 'Beep' ); 50 // returns 'Beep Boop ' 51 52 out = substringBeforeLast( str, 'Boop' ); 53 // returns 'Beep ' 54 ``` 55 56 </section> 57 58 <!-- /.usage --> 59 60 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 61 62 <section class="notes"> 63 64 ## Notes 65 66 - If a substring is not present in a provided string, the function returns the input string. 67 - If provided an empty substring, the function returns the input string. 68 69 </section> 70 71 <!-- /.notes --> 72 73 <!-- Package usage examples. --> 74 75 <section class="examples"> 76 77 ## Examples 78 79 <!-- eslint no-undef: "error" --> 80 81 ```javascript 82 var substringBeforeLast = require( '@stdlib/string/substring-before-last' ); 83 84 var str = 'beep boop'; 85 var out = substringBeforeLast( str, ' ' ); 86 // returns 'beep' 87 88 out = substringBeforeLast( str, 'e' ); 89 // returns 'be' 90 91 out = substringBeforeLast( str, 'x' ); 92 // returns 'beep boop' 93 94 out = substringBeforeLast( str, '' ); 95 // returns 'beep boop' 96 ``` 97 98 </section> 99 100 <!-- /.examples --> 101 102 <!-- Section for describing a command-line interface. --> 103 104 * * * 105 106 <section class="cli"> 107 108 ## CLI 109 110 <!-- CLI usage documentation. --> 111 112 <section class="usage"> 113 114 ### Usage 115 116 ```text 117 Usage: substring-before-last [options] --search=<string> [<string>] 118 119 Options: 120 121 -h, --help Print this message. 122 -V, --version Print the package version. 123 --search string Search string. 124 --split sep Delimiter for stdin data. Default: '/\\r?\\n/'. 125 ``` 126 127 </section> 128 129 <!-- /.usage --> 130 131 <!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 132 133 <section class="notes"> 134 135 ### Notes 136 137 - If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes. 138 139 ```bash 140 # Not escaped... 141 $ echo -n $'foo\nbar\nbaz' | substring-before-last --search a --split /\r?\n/ 142 143 # Escaped... 144 $ echo -n $'foo\nbar\nbaz' | substring-before-last --search a --split /\\r?\\n/ 145 ``` 146 147 - The implementation ignores trailing delimiters. 148 149 </section> 150 151 <!-- /.notes --> 152 153 <!-- CLI usage examples. --> 154 155 <section class="examples"> 156 157 ### Examples 158 159 ```bash 160 $ substring-before-last abcdefg --search d 161 abc 162 ``` 163 164 To use as a [standard stream][standard-streams], 165 166 ```bash 167 $ echo -n $'beep\nboop' | substring-before-last --search p 168 bee 169 boo 170 ``` 171 172 By default, when used as a [standard stream][standard-streams], the implementation assumes newline-delimited data. To specify an alternative delimiter, set the `split` option. 173 174 ```bash 175 $ echo -n 'beep\tboop' | substring-before-last --search p --split '\t' 176 bee 177 boo 178 ``` 179 180 </section> 181 182 <!-- /.examples --> 183 184 </section> 185 186 <!-- /.cli --> 187 188 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 189 190 <section class="references"> 191 192 </section> 193 194 <!-- /.references --> 195 196 <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> 197 198 <section class="related"> 199 200 * * * 201 202 ## See Also 203 204 - <span class="package-name">[`@stdlib/string/substring-before`][@stdlib/string/substring-before]</span><span class="delimiter">: </span><span class="description">return the part of a string before a specified substring.</span> 205 - <span class="package-name">[`@stdlib/string/substring-after`][@stdlib/string/substring-after]</span><span class="delimiter">: </span><span class="description">return the part of a string after a specified substring.</span> 206 - <span class="package-name">[`@stdlib/string/substring-after-last`][@stdlib/string/substring-after-last]</span><span class="delimiter">: </span><span class="description">return the part of a string after the last occurrence of a specified substring.</span> 207 208 </section> 209 210 <!-- /.related --> 211 212 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 213 214 <section class="links"> 215 216 [standard-streams]: https://en.wikipedia.org/wiki/Standard_streams 217 218 [mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions 219 220 <!-- <related-links> --> 221 222 [@stdlib/string/substring-before]: https://github.com/stdlib-js/string/tree/main/substring-before 223 224 [@stdlib/string/substring-after]: https://github.com/stdlib-js/string/tree/main/substring-after 225 226 [@stdlib/string/substring-after-last]: https://github.com/stdlib-js/string/tree/main/substring-after-last 227 228 <!-- </related-links> --> 229 230 </section> 231 232 <!-- /.links -->