Norman
Norman

Reputation: 6365

PHP's preg_replace in JavaScript

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);

?>

Fiddle

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);

JsFiddle

Upvotes: 0

Views: 446

Answers (1)

u_mulder
u_mulder

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

Related Questions