首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >替换数组中多个字符串上的单词

替换数组中多个字符串上的单词
EN

Stack Overflow用户
提问于 2018-05-31 06:28:16
回答 3查看 47关注 0票数 0

我正在尝试弄清楚如何用随机数替换数字/字母排列的实例,在多个字符串中,在一个数组中,同时在每次单击时随机生成其中一个字符串。

本质上,我希望在单击时从我的数组中随机生成一个字符串,并用随机数替换'6a‘。

我已经能够在每次点击时生成一个随机的字符串,如果我删除了数组中的所有其他字符串,我可以替换字符串中的数字,但我不能让所有东西都一起工作。这就是我的代码到目前为止的样子!

提前感谢您的帮助!

//strings that I want to generate and change

var events = ['String one 6a',
  
'String two 6a',

'String three 6a',

'String four 6a',

'String five 6a',

];

//function click generating new random string

function newEvent() {
  var randomNumber = Math.floor(Math.random() * (events.length));
  var b = randomNumber;
  document.getElementById("thing").innerHTML = events[b];
};

//attempt to change part of a string to a random number

function numChange() {
  for (var i = 0; i < events.length; i++) {
      events[i] = events[i].replace('6a', function(match) {
        Math.floor(Math.random() * 7);
      } );
  };
} 
<body>
  <div id="thing">
    <p>
      We're replacing this thing
    </p>
  </div>
  <button onclick="newEvent()">
    Click me
  </button>
</body>

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-05-31 06:40:50

如果您可以使用字符串控制数组,请执行此操作。

var events = [
  'String one ',
  'String two ',
  'String three ',
  'String four ',
  'String five '
];

function newEvent() {
  var randomArrayString = Math.floor(Math.random() * (events.length));
  var randomNumber = Math.floor(Math.random() * 10)
  document.getElementById("thing").innerHTML = events[randomArrayString] + randomNumber;
};
<body>
  <div id="thing">
    <p>
      We're replacing this thing
    </p>
  </div>
  <button onclick="newEvent()">
    Click me
  </button>
</body>

如果您无法控制数组字符串,请在str.replace()中使用regexp

var events = [
'String one 6a',
  'String two 6a',
  'String three 6a',
  'String four 6a',
  'String five 6a'
];

function newEvent() {
  var randomArrayId = Math.floor(Math.random() * (events.length));
  var randomNumber = Math.floor(Math.random() * 10)
  document.getElementById("thing").innerHTML = events[randomArrayId].replace(/6a/,randomNumber);
};
<body>
  <div id="thing">
    <p>
      We're replacing this thing
    </p>
  </div>
  <button onclick="newEvent()">
    Click me
  </button>
</body>

票数 0
EN

Stack Overflow用户

发布于 2018-05-31 07:07:17

这里有一个干净而简单的解决方案:

let events = ['String one 6a',
  
'String two 6a',

'String three 6a',

'String four 6a',

'String five 6a',

];

function numChange() { 
  events = events.map(event => event.replace('6a', Math.floor(Math.random() * 7)));
};

function newEvent() {
  let randomNumber = Math.floor(Math.random() * (events.length));
  document.getElementById("thing").innerHTML = events[randomNumber]
};

//execute once to replace '6a' with a random number
numChange();
<body>
  <div id="thing">
    <p>
      We're replacing this thing
    </p>
  </div>
  <button onclick="newEvent()">
    Click me
  </button>
</body>

票数 0
EN

Stack Overflow用户

发布于 2018-05-31 06:59:30

应该是这样的:

//<![CDATA[
/* external.js */
var doc, bod, htm, M, I, S, Q, old = onload; // for use on other loads
onload = function(){
if(old)old(); // change old var name if using technique on other pages
doc = document; bod = doc.body; htm = doc.documentElement;
M = function(tag){
  return doc.createElement(tag);
}
I = function(id){
  return doc.getElementById(id);
}
S = function(selector, within){
  var w = within || doc;
  return w.querySelector(selector);
}
Q = function(selector, within){
  var w = within || doc;
  return w.querySelectorAll(selector);
}
var events = ['String one 6a', 'String two 6a', 'String three 6a', 'String four 6a', 'String five 6a'];
I('f').onsubmit = function(){
  return false;
}
function randEventRep(){
  return events[Math.floor(Math.random()*events.length)].replace(/\b6a\b/g, Math.floor(Math.random()*7));
}
var press = I('press'), thing = I('thing');
press.onclick = function(){
  thing.innerHTML = randEventRep();
}
} // end load
//]]>
/* external.css */
html,body{
  padding:0; margin:0;
}
body{
  background:#000; overflow-y:scroll;
}
.main{
  width:940px; background:#ccc; padding:20px; margin:0 auto;
}
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
  <head>
    <meta http-equiv='content-type' content='text/html;charset=utf-8' />
    <meta name='viewport' content='width=device-width' />
    <title>Test Template</title>
    <link type='text/css' rel='stylesheet' href='external.css' />
    <script type='text/javascript' src='external.js'></script>
  </head>
<body>
  <div class='main'>
    <form id='f' name='f'>
      <p id='thing'>This is the thing we&apos;re changing</p>
      <input id='press' type='button' value='Click Me' />
    </form>
  </div>
</body>
</html>

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50614165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档