javascript正则表达式不适用于免费web服务
我有以下代码,我想开发一个像rubular.com这样的web服务,但我不知道确切的regex命令 我希望当使用print\r查看时,结果会像php那样返回,就像数组结构一样 应用程序也可从以下网址获得: 这就是它开裂的地方,因为它不会返回preg_match_alljavascript正则表达式不适用于免费web服务,javascript,regex,Javascript,Regex,我有以下代码,我想开发一个像rubular.com这样的web服务,但我不知道确切的regex命令 我希望当使用print\r查看时,结果会像php那样返回,就像数组结构一样 应用程序也可从以下网址获得: 这就是它开裂的地方,因为它不会返回preg_match_all var regex = new RegExp(document.getElementById('regex').value, document.getElementById('regex_params').value);
var regex = new RegExp(document.getElementById('regex').value, document.getElementById('regex_params').value);
var result = regex.exec(document.getElementById('string').value);
如何将结果返回为preg_match_all does in php
应用程序代码:
<html>
<head>
<title>RegEx</title>
<script type="text/javascript">
function timeout_trigger() {
document.getElementById('result').innerHTML = document.getElementById('regex').value + ' ' + document.getElementById('regex_params').value + ' ' + document.getElementById('string').value;
var regex = new RegExp(document.getElementById('regex').value, document.getElementById('regex_params').value);
var result = regex.exec(document.getElementById('string').value);
document.getElementById('result').innerHTML = result;
}
document.onkeyup = KeyCheck;
function KeyCheck()
{
if((document.getElementById('regex').value!='')&&(document.getElementById('regex_params').value!='')&&(document.getElementById('string').value!=''))
{
setTimeout('timeout_trigger()', 1000);
}
else document.getElementById('result').innerHTML = "null";
}
</script>
<style type="text/css">
table
{
background-color:#eeeeee;
}
input
{
height:40px;
padding:5px 5px 5px 5px;
background-color:#000000;
border:1px solid #ffffff;
color:#ffffff;
}
textarea
{
background-color:#000000;
border:1px solid #ffffff;
color:#ffffff;
padding:5px 5px 5px 5px;
}
</style>
</head>
<body bgcolor="#000000">
<table align="center" valign="top" width="1000px" cellpadding="5px" cellspacing="5pc" border="0" bgcolor="#ffffff">
<tr>
<td colspan="4">Your regular expression:</td>
</tr>
<tr>
<td width="5px"><b>/</b></td>
<td><input type="text" name="regex" id="regex" size="135" /></td>
<td width="5px"><b>/</b></td>
<td width="20px"><input type="text" name="regex_params" id="regex_params" size="5" /></td>
</tr>
<tr>
<td colspan="2">Your text string:</td>
<td colspan="2">Match result:</td>
</tr>
<tr>
<td colspan="2" valign="top"><textarea name="string" id="string" rows="10" cols="40"></textarea></td>
<td colspan="2" valign="top"><div id="result"></div></td>
</tr>
</table>
</body>
</html>
正则表达式
函数超时\u触发器(){
document.getElementById('result').innerHTML=document.getElementById('regex').value+''+document.getElementById('regex_参数').value+''+document.getElementById('string').value;
var regex=new RegExp(document.getElementById('regex').value,document.getElementById('regex_参数').value);
var result=regex.exec(document.getElementById('string').value);
document.getElementById('result')。innerHTML=result;
}
document.onkeyup=KeyCheck;
函数KeyCheck()
{
if((document.getElementById('regex').value!='')和&(document.getElementById('regex_参数').value!='')和&(document.getElementById('string').value!='')
{
setTimeout('timeout_trigger()',1000);
}
else document.getElementById('result').innerHTML=“null”;
}
桌子
{
背景色:#eeeeee;
}
输入
{
高度:40px;
填充物:5px 5px 5px;
背景色:#000000;
边框:1px实心#ffffff;
颜色:#ffffff;
}
文本区
{
背景色:#000000;
边框:1px实心#ffffff;
颜色:#ffffff;
填充物:5px 5px 5px;
}
您的正则表达式:
/
/
您的文本字符串:
比赛结果:
在JavaScript中匹配正则表达式将只返回第一个匹配项,但它将返回所有子模式
如果添加
g
标志,则它将返回所有匹配项,但仅丢失整个匹配文本和子模式。如果要搜索与PHP等价的javascript,请使用:
其中,
模式
是一个带有全局标志的模式。以下内容将给出与rubular大致相同的输出,它总是在firefox中添加g选项(全局),而其他浏览器则不知道
function timeout_trigger() {
document.getElementById('result').innerHTML =
document.getElementById('regex').value + ' ' +
document.getElementById('regex_params').value + ' ' +
document.getElementById('string').value;
var regex = new RegExp(document.getElementById('regex').value,
"g" + document.getElementById('regex_params').value);
var groups=new Array();
var matchCounter=1;
var result=document.getElementById('string').value.replace(regex,
function(a){
var foundMatch=false;
var argcount=1;
var tmp=new Array();
while(argcount<arguments.length-2){
foundMatch=true;
tmp[tmp.length]=argcount;
tmp[tmp.length]=":";
tmp[tmp.length]=arguments[argcount];
tmp[tmp.length]="<br />";
argcount++;
}
if(foundMatch){
groups[groups.length]="Match " + matchCounter +":<br />"+
tmp.join("");
matchCounter++;
}
return "<font style='background-color:#ff0000'>" + a + "</font>";
});
document.getElementById('result').innerHTML = result + "<br />" +
groups.join("");
}
函数超时\u触发器(){
document.getElementById('result')。innerHTML=
document.getElementById('regex')。值+''
document.getElementById('regex_params')。value+''
document.getElementById('string')。值;
var regex=new RegExp(document.getElementById('regex')。值,
“g”+document.getElementById('regex_params').value);
变量组=新数组();
var匹配计数器=1;
var result=document.getElementById('string').value.replace(regex,
职能(a){
var foundMatch=false;
var argcount=1;
var tmp=新数组();
虽然(argcount如何将结果返回为preg_match_allpreg_match_all
是一个PHP函数。它与JavaScript无关。preg_match_all是什么?这是一个PHP函数吗?你的问题甚至没有标记为PHP,没有理由在这里使用PHP,因为JavaScript可以执行正则表达式测试。不,我想要与pr等效的例如来自phpdocument.getElementById('result')的匹配.innerHTML=result;
您正试图将javascript数组作为html填充到dom节点中。祝您好运……没有不丢失任何数据的方法吗?不幸的是,没有。javascript的正则表达式函数是在假定您事先知道要做什么,而不是接受任意用户输入的情况下设计的ser请求,没问题,代码在浏览器端执行;我只想创建与rubular doesi相同的服务:函数preg_match_all(regex,haystack,params){var globalRegex=new RegExp(regex,params);var globalMatch=haystack.match(globalRegex);matchArray=new Array();for(globalMatch中的变量i){nonGlobalRegex=new RegExp(regex);nonGlobalMatch=globalMatch[i]。match(nonGlobalRegex);matchArray.push(nonGlobalMatch[1]);}返回matchArray;}是的,如果你想让匹配的数组包含反引用数组和所有的数组,你需要多次应用正则表达式
function timeout_trigger() {
document.getElementById('result').innerHTML =
document.getElementById('regex').value + ' ' +
document.getElementById('regex_params').value + ' ' +
document.getElementById('string').value;
var regex = new RegExp(document.getElementById('regex').value,
"g" + document.getElementById('regex_params').value);
var groups=new Array();
var matchCounter=1;
var result=document.getElementById('string').value.replace(regex,
function(a){
var foundMatch=false;
var argcount=1;
var tmp=new Array();
while(argcount<arguments.length-2){
foundMatch=true;
tmp[tmp.length]=argcount;
tmp[tmp.length]=":";
tmp[tmp.length]=arguments[argcount];
tmp[tmp.length]="<br />";
argcount++;
}
if(foundMatch){
groups[groups.length]="Match " + matchCounter +":<br />"+
tmp.join("");
matchCounter++;
}
return "<font style='background-color:#ff0000'>" + a + "</font>";
});
document.getElementById('result').innerHTML = result + "<br />" +
groups.join("");
}