
Use isNaN ( )and, if it returns false, then it's a numeric value. You could add an additional clause to the if to check is a number. The sed solution would be portable to all Unix-like systems. Re: Regular Expressions in JavaScript not working. Note that if there are several instances of prefix on a line, the sed variation would return the string after the last one, while the GNU grep variation would return the string after the first one (which would include the other instances of prefix). The substitution will replace the whole line that matches the expression (which is a BRE), with the piece of it that occurs after the string prefix. What this does is to only print the lines that sed manages to apply the given substitution to. If you are not using GNU grep, then you may use sed instead to get the bit between the string prefix and the end of the line: sed -n 's/.*prefix\(.*\)/\1/p' file Note that both -P and -o are non-standard extensions the POSIX specification of grep. Again, with GNU grep (and some other grep implementations), you may use the -o option to get only the bit(s) that matches the given expression from each line.

If you use GNU grep, you would be able to use Perl-like regular expressions if you used grep with the GNU grep-specific -P option.Īlso note that grep returns lines by default, not substrings from lines. See the manual for re_format or regex or whatever similar manual your grep manual refers to on your system, or the POSIX standard texts that I just linked to. These are basic regular expressions (BRE) and extended regular expressions (ERE, if grep is used with the -E option).

However, they’re situations where it’s best to use other tools instead of RegExp. So far, we’ve explored regular expressions, how they work in JavaScript and why we should use them.

What you are showing is a Perl-like regular expression (PCRE, "Perl Compatible Regular Expression"). Regular Expressions, When Not to Use Them. Regular expressions come in many different flavours.
