Reputation: 179
I'm not good at regex so I need some help.
I'm trying to find how to get for example 10 characters before the pattern and 10 characters after. like:
dskjgh kdsfhgl:ks;dhfl\kgh.sldfgdfgsd<pattern1.fso[wpe;x,/c,zasdfsdg
(10 chars before pattern1) var1 = "ldfgdfgsd<"
(10 chars after pattern1) var2 = ".fso[wpe;x"
Upvotes: 1
Views: 1930
Reputation: 647
$str = "dskjgh kdsfhgl:ks;dhfl\\kgh.sldfgdfgsd<pattern1.fso[wpe;x,/c,zasdfsd";
preg_match( "/(.{10})(pattern1)(.{10})/", $str, $matches);
$var1 = $matches[1];
echo $var1;
$var2 = $matches[3];
echo $var2;
Upvotes: 3
Reputation: 14233
This should do the trick:
var str = "lasdjfl askjdfl askjdf kljlajpattern1lakjsdflajsdlfkj";
var match = str.match(/(.{10})pattern1(.{10})/i);
var text1 = match[1];
var text2 = match[2];
alert(text1);
alert(text2);
Results:
text1 = jdf kljlaj
text2 = lakjsdflaj
Upvotes: 2