cfpete
cfpete

Reputation: 4443

Getting the text between delimiters with regex

How can I get the contents between two delimiters using a regular expression? For example, I want to get the stuff between two |. For example, for this input:

|This is the text I want|

it should return this:

This is what I want

I already tried /^|(.*)$|/, but that returns This is what I want | instead of just This is what I want (shouldn't have the | at the end)

Upvotes: 0

Views: 93

Answers (1)

maerics
maerics

Reputation: 156434

Try escaping the pipes /\|(.*?)\|/

For example, using JavaScript:

var s = '| This is what I want |';
var m = s.match(/\|\s*(.*?)\s*\|/);
m[1]; // => "This is what I want"

See example here: https://regex101.com/r/qK6aG2/2

Upvotes: 3

Related Questions