如何在程序中获得定义的值? [英] How can I get defined value in my program ?
本文介绍了如何在程序中获得定义的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将我的书面文字替换为javascript原始语法,以减少写入和节省时间,我接近它,但初学者的许多问题,我已经坚持使用RegExp()方法,我只知道如何找到像这样的符号\\(或/ \(/
但我不明白为什么代码不起作用只是看我的脚本并回答我。
我尝试过:
< !DOCTYPE html >
< html >
< 正文 >
< textare a type = text id = in1 行 = 4 >
< / textarea >
< br >
< textarea type = text id = in2 >
< / textarea >
< span class =code-keyword>< script >
function replace()
{
/ * 此代码将始终显示textarea的底线* /
var ta = document .getElementById( in1);
ta.scrollTop = ta.scrollHeight;
var in2 = document .getElementById( in2)。value;
/ * 这会将所有字符替换为其他字符* /
var replaceChars =
{
d。: document。,
b。: body。,
w \\( : write(,
wl \\(: writeln(,
ce\\(: c reateElement(,
ctn \\(:< span class =code-string> createTextNode(,
ac\\(: appendChild(
};
var re = new RegExp (Object.keys(replaceChars).join( |), g);
var replacement = in2.replace(re, function (match)
{ return replaceChars [match];});
document .getElementById( in1)。value = replacement;
};
/ * 如果我输入第二个textaread.w();它返回document.undefined);我想要document.write();如何解决这个未定义的问题* /
< / script >
< / body >
< / html >
解决方案
你的问题是的游行w'\\\(
是w(
这不在您的数组中。该匹配与RegEx不同。
另一个错误:
d。
和b。
不匹配d或b后跟一个点,它们匹配d或b后面跟着任何东西,一个字母,一个数字,一个空格...
以下是RegEx文档的链接:
perlre - perldoc.perl.org [ ^
i m replacing my written words to javascript original syntax for less writing and saving time, i m getting near to it but many problems for a beginner, i have stuck here with RegExp() method where i just knew that how to find symbols like "\\(" or /\(/
but i dont understand why the code doesn't work just watch my script and answer me.
What I have tried:
<!DOCTYPE html>
<html>
<body>
<textarea type= "text" id = "in1" rows = "4">
</textarea>
<br>
<textarea type= "text" id = "in2" >
</textarea>
<script>
function replace()
{
/* this code will always show bottom line of textarea */
var ta = document.getElementById("in1");
ta.scrollTop = ta.scrollHeight;
var in2 = document.getElementById("in2").value;
/* this will replace all characters to onother characters */
var replaceChars =
{
"d.": "document.",
"b." : "body.",
"w\\(": "write(",
"wl\\(" : "writeln(",
"ce\\(" : "createElement(",
"ctn\\(" : "createTextNode(",
"ac\\(" : "appendChild("
};
var re = new RegExp(Object.keys(replaceChars).join("|"),"g");
var replaced = in2.replace(re ,function (match)
{return replaceChars[match];});
document.getElementById("in1").value = replaced;
};
/* if i type in second textarea "d.w();" it returns "document.undefined);" and i want "document.write();" how to solve this undefined problem */
</script>
</body>
</html>
解决方案
Your problem is that the march forw\\(
isw(
which is not in your array. The match is different from the RegEx.
Another bug:
d.
andb.
are not matching "d" or "b" followed by a point, they match "d" or "b" followed by anything, a letter, a number, a space ...
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
这篇关于如何在程序中获得定义的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文