我不擅长正则表达式,所以我需要一些帮助。
我试图找到如何在模式之前获取例如 10 个字符和之后获取 10 个字符。像:
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"
$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;
这应该可以解决问题:
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);
结果:
text1 = jdf kljlaj
text2 = lakjsdflaj