如何在Javascript中执行preg_replace?
我在PHP中执行以下操作,但希望将其移至Javascript。
<?php
$errors = '::Could not sign you into your account! ';
$errors .= '::Email or password error! ';
$errors = preg_replace('#\::(.+?)(?![^::])#','<div>$1</div>',$errors);
echo ($errors);
?>
Fiddle
我尝试使用JavaScript进行相同的操作,但是以某种方式无法正常工作。关于如何做到这一点的任何想法?
var theString = "::Could not sign you into your account! :: Email or Password Error! ";
theString = theString.replace('#\::(.+?)(?![^::])#/g','<div>$1</div>');
alert(theString);
JsFiddle
最佳答案
Javascript正则表达式通常不使用引号和#
:
var theString = "::Could not sign you into your account! :: Email or Password Error! ";
theString = theString.replace(/\::(.+?)(?![^::])/g,'<div>$1</div>');
alert(theString);
小提琴:https://jsfiddle.net/rhtfzmxd/
关于javascript - JavaScript中的PHP preg_replace,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35194014/