Home  >  Article  >  Backend Development  >  javascript - 判断某一个字符串是否完全匹配的正则该怎样写??

javascript - 判断某一个字符串是否完全匹配的正则该怎样写??

WBOY
WBOYOriginal
2016-06-06 20:08:071653browse

<code>  <textarea name="val">
   白色:80;黑色:90;绿色:100;红色:-80;
  </textarea>
  <input type="button" value="check">

<script>

// 怎么检测textarea的值完全符合(每一个键值对都需要符合下面这种格式):
     白色:80;
 这种格式??
 
 
</script>
</code>

回复内容:

<code>  <textarea name="val">
   白色:80;黑色:90;绿色:100;红色:-80;
  </textarea>
  <input type="button" value="check">

<script>

// 怎么检测textarea的值完全符合(每一个键值对都需要符合下面这种格式):
     白色:80;
 这种格式??
 
 
</script>
</code>

<code class="javascript">var re = /^([^:\s;]+:-?\d+;)*$/mg;
console.log('白色:60;黑色:50;'.match(re));
console.log('白色:60;;黑色:50;'.match(re));
console.log('蛤蛤:-1;'.match(re));
console.log(''.match(re));</code>

调试在这里 https://regex101.com/r/jK6zG2/2 (会有点慢, 耐心等待加载完)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn