0

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

1

1 Answer 1

3

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.