简体   繁体   English

像正则表达式一样将字符串转换为 javascript 中的 object

[英]convert a string to object in javascript like regular expression

I have the following variable我有以下变量

var characters = /[-()&]/

and i have a string like this我有一个这样的字符串

var characters_string = "/[-()&]/"

this string is result from array and join, because i need to add news variables, so, i need to convert characters_string to a object like characters because is not working with characters_string because when i use in this line这个字符串是数组和连接的结果,因为我需要添加新闻变量,所以,我需要将 characters_string 转换为 object 之类的字符,因为不使用 characters_string 因为当我在这一行中使用时

var filter_d = info.split(/[.,%]/)

and i use it like this我像这样使用它

var filter_d = info.split(characters_string)

but i get error但我得到错误

thanks谢谢

You can use new RegExp ,but you need delete the "/" at the start and the end of the string您可以使用new RegExp ,但您需要删除字符串开头和结尾的“/”

new RegExp(characters_string.slice(1, -1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM