0

Essentially this code searches through a string, finds and replaces according to the each of these regex patterns before spitting the updated variable back.

I'm having difficulty translating it from PHP code to Javascript:

in PHP I have achieved it as such:

$dataPreg = array(
'/(.*)\s§/' => '$1 <span class="suppSmall">quasi</span>',
'/(\bor|and\b)/'=>'<span class="orChar">$1</span>',
'/\:(\s*see[\:]?)/'=>'<span class="orChar"> $1: </span>'
);

$root = preg_replace(array_keys($dataPreg),array_values($dataPreg),$root);

return $root;

in Javascript I attempted to port it as such:

var dataPreg = {
'/(.*)\s§/':'$1 <span class="suppSmall">quasi</span>',
'/(\bor|and\b)/':'<span class="orChar">$1</span>',
'/\:(\s*see[\:]?)/':'<span class="orChar"> $1: </span>'
};

for (var val in dataPreg)
$string = $string.replace(new RegExp(val, "g"), dataPreg[val]);
return $string;

Needless to say, its not working in Javascript and I can't figure out why. It doesn't trigger any JS errors, it simply returns the string back..unchanged.

3 Answers 3

1

Keys of Objects should be Strings, so I suggest you saving effort and cycles by using an Array of Objects, i.e.

var dataPreg = [
    {re: /(.*)\s§/g        , sub: '$1 <span class="suppSmall">quasi</span>'},
    {re: /(\bor|and\b)/g   , sub: '<span class="orChar">$1</span>'},
    {re: /\:(\s*see[\:]?)/g, sub: '<span class="orChar"> $1: </span>'}
];

Then

(function () {
    var i; // don't pollute namespace ;)
    for (i = 0; i < dataPreg.length; ++i)
        $string = $string.replace(dataPreg[i].re, dataPreg[i].sub);
}());

e.g.

var $string = 'foo § bar or then and you :see: here';
/* becomes
foo <span class="suppSmall">quasi</span> bar <span class="orChar">or</span> then <span class="orChar">and</span> you <span class="orChar"> see:: </span> here
*/
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Paul very clever, worked like a charm and very easy to read.
1

The RegExp takes the pattern as parameter without the delimiter. For example it takes [a-z]+ as parameter, and not take /[a-z]+/ like you do it in PHP. So remove the // delimiters from the both end of your regex. One example:

'(.*)\s§':'$1 <span class="suppSmall">quasi</span>',
 ^ the / is gone

1 Comment

Thanks--I did try that, however, it appears as though it has no effect--something else is also to blame..
1

You array strings aren't right for creating new RegExp object. (Using delimiters and single escaped propertied e.g. \s instead of \\s).

Try this instead:

dataPreg = {
'/(.*)\\s§/g':'$1 <span class="suppSmall">quasi</span>',
'/(\\bor|and\\b)/g':'<span class="orChar">$1</span>',
'/:(\\s*see:?)/g':'<span class="orChar"> $1: </span>'
};
for (var val in dataPreg)
    $string = $string.replace(val, dataPreg[val]);
return $string;

4 Comments

I just tested it, it didn't work, but the opposite did! I escaped everything with double slashes.. so instead of \s I put it as \\s --I'm guessing that's what you meant? Thanks
Whether regex is right or not I can't tell since you haven't provided value of $string
I'm referring to your advice (to use single escaped..)--I double escaped and it worked :) and I'm guessing that's what you meant to tell me
' '.replace('/\\s/', '') !== ''

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.