1

I have some regex patterns stored as strings inside a PHP array. I need them to be outputted as JavaScript RegExp objects. Is there any way to do this by using PHP's json_encode?

Example:

$array = [
   'regex' => '/some-regex/' 
];
return json_encode( $array );

Desired output:

{"regex": /some-regex/}

Current output:

{"regex": "/some-regex/"}
9
  • 3
    You cannot create JavaScript data structures using PHP, no. Commented Jun 18, 2019 at 15:15
  • 6
    {"regex": /some-regex/} is not valid JSON. Commented Jun 18, 2019 at 15:16
  • 2
    You can still use it even if it is a string by passing it in the constructor of RegExp. Example: let pattern = new RegExp(theRegexString) and then use pattern instead of the regex. Commented Jun 18, 2019 at 15:20
  • @AbraCadaver (yikes :-) - deriving from the OPs terminology, once converted from JSON to a JavaScript object, it is valid to have {regex: new RegExp(/some/)}. In otherwords, have a RegExp Object as an object property. Commented Jun 18, 2019 at 15:20
  • 1
    Right - oh, I made the first comment :-). Going on and on about using new Regexp() isn't necessary. Commented Jun 18, 2019 at 15:24

2 Answers 2

4

Not directly with json_encode. All PHP does is create arrays and objects containing simple scalar types. It cannot create JS objects like RegExp or call JS functions directly. If all you're going to pass is that sample data in the question you can always do

$array = [
   'regex' => '/some-regex/' 
];
$js_string = '';
foreach($array as $key => $value) $js_string .= '"' . $key . '": new RegExp("' . $value ' "),';
return '{' . $js_string . '}';

That's really clunky code and, of course, it falls apart if $array contains anything but regular expressions (and it assumes that everything in there is safe for JS). But if that's all you have to do...

Wait! Is there a better way?

Indeed there is. The better thing to do is have json_encode do it's thing (it's kinda made to handle JSON) and then have your JS create the objects itself

let regex_list = <?php echo json_encode($array) ?>;
let myregex = {};
regex_list.forEach(function(regex, index) {
    myregex[index] = new RegExp(regex);
});

This is a much cleaner solution. We don't have to worry about the data coming from PHP, so JS can take it from there and build a JS object that holds all the RegExp objects you want to create

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

Comments

1

No, you can't. Because you're passing through JSON structure which just wraps-data. A string by json standard starts with " and ends with ", otherwise it's a number or an array for istance.

I wondered a similiar thing about it. I wanted to create a function for JS but on server side with php, based on the result. Usually when you face this problem it means you've to rethink a bit the structure of your page/app.

However as they pointed out in the comments to your question, you might find workarounds to build the object locally once the HTTP request is ended.

Also would recommend reading this: Security issues JSON with eval

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.