Php脚本到javascript
我正在尝试用javascript编写类似的东西Php脚本到javascript,php,javascript,scripting,Php,Javascript,Scripting,我正在尝试用javascript编写类似的东西 function Spin($txt){ $test = preg_match_all("#\{(.*?)\}#", $txt, $out); if (!$test) return $txt; $toFind = Array(); $toReplace = Array(); foreach($out[0] AS $id => $match){ $choices = explode(”|”, $out[1][$id]); $toFind
function Spin($txt){
$test = preg_match_all("#\{(.*?)\}#", $txt, $out);
if (!$test) return $txt;
$toFind = Array();
$toReplace = Array();
foreach($out[0] AS $id => $match){
$choices = explode(”|”, $out[1][$id]);
$toFind[]=$match;
$toReplace[]=trim($choices[rand(0, count($choices)-1)]);
}
return str_replace($toFind, $toReplace, $txt);
但不知道从哪里开始-如果有人可以帮助我,请帮助
我有以下的输入:
{关键字1 |关键字2 |关键字3}{单词1 |单词2 |单词3}{测试1,测试2,测试3}
脚本的目的是将
- [关键词1][word 1][test 1]
- [关键词2][和单词2][测试2]
- [关键词3][单词3][测试3]
然后要组合字符串…查找字符串上的拆分方法 数组上的and和join方法
在google中搜索Javascript拆分和Javascript连接(单独查询)。w3schools链接非常有用输入和输出对我来说都没有意义,但这应该会将您推向正确的方向:
var input = '{keyword 1 | keyword 2 | keyword 3} {word 1 | word 2 | word 3} {test 1, test 2, test 3}';
var matches = input.match(/\{(.*?)\}/ig); // grab stuff in curly braces
var choices = matches[0].replace('{', '') // remove left curly brace
.replace('}', '') // remove right curly brace
.split(' | '); // split into array
选项
将成为一个数组
['keyword 1', 'keyword 2', 'keyword 3']
您可以对单词和测试执行相同的操作,然后可以根据需要将它们组合起来。请解释函数的作用以及您的问题所在。到目前为止,您尝试过什么吗?与其抛售代码并期望有人完成所有工作,您至少可以概述您正在尝试实现的目标-尤其是您的代码甚至没有注释。谢谢Amit;-)我现在就试试,并会让你知道进展