简体   繁体   English

Javascript如何在字符串中查找子字符串并查看图像

[英]Javascript How to find a sub-string in the string and view an image

I apologize for the English language. 我为英语表示歉意。 I will try to search the sub-string: Example: Hello world, my name is Peter. 我将尝试搜索子字符串:示例:世界,您好,我叫Peter。

  • 1.case - if "Peter" is in the string - Figure 1 is displayed 1.案例-如果字符串中包含“ Peter”-将显示图1
  • 2nd case - if there is a "name" in the string - also picture 2 is displayed 第二种情况-如果字符串中有“名称”-还会显示图片2

data are entered via html input The options is about 70 数据通过html输入输入。选项约为70

I use the following code to view the image. 我使用以下代码查看图像。

function change() {
  var text = "";
  var image = "";
  //alert('hi');
  var answers = str.match(document.getElementById("UserInputImage").value);
  //alert(answers);
  switch (answers) {
    case "2":
      text = "Obrázok porovnania";
      image = '<img src="https://qlis.darden.sk/img_por/1_qr.jpg"></img>';
      break;
    case "3":
      text = "NoteB";
      image = '<img src="http://fc01.deviantart.net/fs29/f/2009/238/d/8/Small_50x50__png_clock_pic_by_counter_countdown_ip.png"></img>';
      break;
    case "C":
      text = "NoteC";
      image = '<img src="http://a.deviantart.net/avatars/r/a/rachelsrandomart.gif?12"></img>';
      break;

    default:
      text = "Nie je naskenovaný QR kód alebo nie je správny";
  }
  document.getElementById("demo1").innerHTML = text;
  document.getElementById("demo2").innerHTML = image;
}

From what I can gather from your updated question, you want the user to input some text into a string, and it then to show images & some text based on certain strings been in the input. 根据我从更新后的问题中收集到的信息,您希望用户将一些文本输入一个字符串,然后根据输入的某些字符串显示图像和一些文本。

If so, what you can do is create a simple array with the regEx for each term you require, and then the text & image for this term. 如果是这样,您可以做的是为您需要的每个术语创建一个带有regEx的简单数组,然后为该术语创建文本和图像。

You can then loop and check, build the found terms into an array and simple map those to your desired output. 然后,您可以循环检查,将找到的术语构建到数组中,并将其简单映射到所需的输出。

Example below.. 下面的例子

ps. PS。 You didn't give a 3rd case, so I've used the term hello for that to trigger. 您没有给出第三种情况,因此我使用了hello这个词来触发。

All you then need to do is update the array with your 70 terms, but that's left for you.. :) 然后,您需要做的就是用70个字词更新数组,但这留给您了.. :)

 const terms = [ { re: /peter/i, txt: "Obrázok porovnania", img: 'https://qlis.darden.sk/img_por/1_qr.jpg' }, { re: /name/i, txt: "NoteB", img: "http://fc01.deviantart.net/fs29/f/2009/238/d/8/Small_50x50__png_clock_pic_by_counter_countdown_ip.png" }, { re: /hello/i, txt: "NoteC", img: "http://a.deviantart.net/avatars/r/a/rachelsrandomart.gif?12" } ]; const userInput = document.getElementById("UserInputImage"); function change() { const found = []; for (const term of terms) { if (this.value.match(term.re)) { found.push(term); } } document.getElementById("demo1").innerHTML = found.map(m => `<p>${m.txt}</p>`).join(""); document.getElementById("demo2").innerHTML = found.map(m => `<img src=${m.img}/>`).join(""); } userInput.addEventListener("input", change); 
 <p>Type -> <b>hello my name is peter</b> to see all terms</p> <input id="UserInputImage"> <div id="demo1"></div> <div id="demo2"></div> 

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

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