Reputation: 6365
How do I do a preg_replace in Javascript?
I do the following in PHP, but would like to move it to Javascript.
<?php
$errors = '::Could not sign you into your account! ';
$errors .= '::Email or password error! ';
$errors = preg_replace('#\::(.+?)(?![^::])#','<div>$1</div>',$errors);
echo ($errors);
?>
I tried doing the same using JavaScript, but it just will not work somehow. Any thoughts on how this can be done?
var theString = "::Could not sign you into your account! :: Email or Password Error! ";
theString = theString.replace('#\::(.+?)(?![^::])#/g','<div>$1</div>');
alert(theString);
Upvotes: 0
Views: 446
Reputation: 54831
Javascript regexps mostly do not use quotes and #
:
var theString = "::Could not sign you into your account! :: Email or Password Error! ";
theString = theString.replace(/\::(.+?)(?![^::])/g,'<div>$1</div>');
alert(theString);
Fiddle: https://jsfiddle.net/rhtfzmxd/
Upvotes: 3