简体   繁体   English

正则表达式只允许不会断开链接的字符

[英]Regular expression allowing only characters that will not break a link

Anybody can help me with regular expression which will only accept alphabetical letters from English alphabet and numbers without whitespaces ( ÖÜÕÖ and similar characters + whitespaces will break the HTML link this thing is creating )? 任何人都可以用正则表达式帮助我,该正则表达式仅接受英语字母和数字中没有空格的字母(ÖÜÕÖ和类似字符+空格会破坏此内容正在创建的HTML链接)? I currently have : /[A-Za-z ]\\S+$/ but this will allow whitespaces and ÖÜÄ and similar at the beggining. 我目前有:/ [A-Za-z] \\ S + $ /,但这在开始时将允许空白和ÖÜÄ等。

function validatenumber(el) {
   var regex = /[A-Za-z ]\S+$/;    
   if( !regex.test(el.value) ) {
      alert('invalid value');
  }else{
          alert('correct value'); 
  }
}

http://jsfiddle.net/qd7BL/1375/ Here's a fiddle. http://jsfiddle.net/qd7BL/1375/这是一个小提琴。

Try 尝试

/^\w+$/

It'll allow only a non empty string, containing english alphabet letter, both cases, underscore and digits in a string. 它只允许一个非空字符串,其中包含英文字母,字符串中的大小写,下划线和数字。

 /^[A-Za-z0-9]+$/ 

Solved the problem, although I should probably think is more through as much more elements are allowed in link. 解决了问题,尽管我可能应该考虑通过链接中允许的更多元素来实现更多功能。

Also, the best solution was to use Slugify, a plugin for jQuery which will make the input correct format. 另外,最好的解决方案是使用Slugify,这是jQuery的插件,可以使输入格式正确。

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

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