0

I have a regular expression like this which extract the content between 2 characters and in this case its between 2 #'s

 (?<=\#)(.*?)(?=\#)

and um using it as follows

var extract = str.match(/(?<=\#)(.*?)(?=\#)/).pop();

but the regex gives errors since I think I need to escape it. How do I correctly apply escape characters for the above regex?

2
  • 2
    Javascript does not support (?<=) Commented May 1, 2015 at 17:31
  • How may I use this in javascript? since regex is kind of universal? Commented May 1, 2015 at 17:32

2 Answers 2

2

Regex may be overkill for this task.

var result = str.split("#")[1] || "";
  • If there is no # in the string, result is the empty string.
  • If there is only one # in the string, result is everything after it.
  • If there are two or more # in the string, result is the substring between the first and second #.
Sign up to request clarification or add additional context in comments.

Comments

1
#(.*?)#

or

#([^#]+)#

Simply use this and grab the group 1.See demo.

https://regex101.com/r/uE3cC4/14

var re = /#(.*?)#/gm;
var str = 'bazbarfoo#asad#';
var m;

while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.