Reputation: 1539
I have a regular expression in PHP and its working fine but i want to use it in js as well can any please tell me how to do that i am not much familiar with it
In php this expression is working like this it allow hyphen symbol (-) between number and character but remove hyphen symbol between characters and replace it with white space Like if i use any of this string
$string ="Al-Abbas - Manama - 100" or
$string ="100 - Al-Abbas - Manama"
$repl = preg_replace('/\d\h*-\h*(*SKIP)(*F)|-(?!\h*\d)/', ' ', $string);
after this i am getting this output
Al Abbas Manama - 100
100 - Al Abbas Manama
i want to do same like this in javascript how can i convert this expression in javascript or any other way to do like this in javascript.
Upvotes: 1
Views: 1134
Reputation: 89557
you can use this pattern that captures what you want to avoid. Since \h
doesn't exists in javascript you can emulate it using [^\S\r\n]
. The replacement function returns the group when it exists or a space:
mystr = mystr.replace(/(\d[^\S\r\n]*-)|-(?![^\S\r\n]*\d)/g, function (_, g) {
return g ? g : ' '; });
Upvotes: 2
Reputation: 715
var str="100 - Al-Abbas - Manama";
var newstr = str.replace(/\d\h*-\h*(*SKIP)(*F)|-(?!\h*\d)/, ' ');
Upvotes: -1