Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use regular expressions to extract strings between fixed characters

Shulou Source: shulou.com Published: 2022-06-02 05:20:58 10月02日 Update

This article mainly introduces how to use regular expressions to extract strings between fixed characters, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let's take a look at it.

The sample code is as follows

/ / extract the string function getInnerString (source, prefix, postfix) {var regexp = new RegExp (encodeReg (prefix) +'. +'+ encodeReg (postfix), 'gi') between fixed characters; var matches = String (source) .match (regexp); var formatedMatches = _ .map (matches, value = > {return value. Replace (prefix,'). Replace (postfix,');}); return formatedMatches } / / escape the regular character function encodeReg (source) {return String (source) .replace (/ ([. * +? ^ =!: ${} () | [\] /\]) / gMagne'\ $1');}

Use:

Var a = 'Let's swing the sculls'; getInnerString (a, 'let's', 'let's'); / / 'sculls'] var b = 'boss and out of the office, please leave a message for Secretary Li'; getInnerString (b, 'leave a message for you',') / / [Secretary Li] Thank you for reading this article carefully. I hope the article "how to use regular expressions to extract strings between fixed characters" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support it. Pay attention to the industry information channel, more related knowledge waiting for you to learn!

Tags: Characters articles between strings regular expressions sculls secretaries codes values interests offices at the same time more friends knowledge examples ribbons boss industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple vpn NVidia macOS MariaDB