Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 有没有办法把点符号转换成括号符号?_Javascript_Vue.js - Fatal编程技术网

Javascript 有没有办法把点符号转换成括号符号?

Javascript 有没有办法把点符号转换成括号符号?,javascript,vue.js,Javascript,Vue.js,我正在尝试转换字符串: string.0.another.0.string 进入 我曾尝试使用RegEx,split(),进行循环,但没有成功 任何帮助都将不胜感激。 let a='string.0.other.0.string'; 让res=''; a、 拆分('.')。forEach(val=>{ res+=val=='0'?'[0].':val; }); 控制台日志(res) let a='string.0.other.0.string'; 让res=''; a、 拆分('.')。for

我正在尝试转换字符串:

string.0.another.0.string
进入

我曾尝试使用RegEx
split()
,进行循环,但没有成功

任何帮助都将不胜感激。

let a='string.0.other.0.string';
让res='';
a、 拆分('.')。forEach(val=>{
res+=val=='0'?'[0].':val;
});
控制台日志(res)
let a='string.0.other.0.string';
让res='';
a、 拆分('.')。forEach(val=>{
res+=val=='0'?'[0].':val;
});

控制台日志(res)使用正则表达式匹配此模式“((任意数字)”,然后替换为用“.”括起来的捕获数字组

const regex=/\.([0-9])\./g;
const str='string.0.other.0.string';
常量subst=“[1]”;
const result=str.replace(regex,subst);

log('替换结果:',结果)使用正则表达式匹配此模式“((任意数字)”,然后替换为用“.”括起来的捕获数字组

const regex=/\.([0-9])\./g;
const str='string.0.other.0.string';
常量subst=“[1]”;
const result=str.replace(regex,subst);

log('替换结果:',结果)假设您的字符串字面上是“string[0]。另一个[0]。string“将支持您

let str=“string.0.other.0.string”;
console.log(str);
str=str.replace(/.0./g,“[0]”);

console.log(str)假设您的字符串字面上是“string[0]。另一个[0]。string“将支持您

let str=“string.0.other.0.string”;
console.log(str);
str=str.replace(/.0./g,“[0]”);

console.log(str)string[0].another[0].string.