我目前正在尝试编写一个输入表单,您可以在其中键入文本并设置其格式以供以后用作 XML 条目。为了使HTML代码XML可读,我必须将代码括号替换为相应的符号代码,即<
替换为<
和 >
与 >
。
格式化文本通过变量 inputtext 传输为 HTML 代码,因此我们有
The <b>Genji</b> and the <b>Heike</b> waged a long and bloody war.
需要转换为的
The <b>Genji</b> and the <b>Heike</b> waged a long and bloody war.
文本,我用 .replace() 函数尝试过:
inputxml = inputxml.replace("<", "<");
inputxml = inputxml.replace(">", ">");
但这只会替换括号第一次出现。我很确定我需要某种循环来实现这一点;我还尝试使用 jQuery 中的 each()
函数(一位朋友建议我查看 jQuery 包),但总的来说,我对编码仍然很陌生,并且很难让它工作。
您将如何编写一个循环来替换如上所述的变量内的代码括号?
其他信息
当然,您的假设是正确的,这是更大的事情的一部分。我是一名日本研究研究生,目前,我正在尝试以更容易理解的方式可视化有关日本历史的信息。为此,我使用了麻省理工学院研究生开发的 Simile Timeline API。您可以在我的主页上查看时间线的工作测试。
Simile Timeline 使用基于 AJAX 和 Javascript 的 API。如果您不想在自己的服务器上安装 AJAX 引擎,您可以实现 MIT 的时间线 API。时间线的数据通常由一个或多个 XML 文件或 JSON 文件提供。就我而言,我使用 XML 文件;您可以查看此示例中的 XML 结构。
在时间轴中,有一些所谓的“事件”,您可以单击这些事件,以便在信息气泡弹出窗口中显示其他信息。这些信息气泡中的文本源自 XML 源文件。现在,如果您想在信息气泡中进行一些 HTML 格式化,则不能使用代码括号,因为它们只会显示为纯文本。但是,如果您使用符号代码而不是普通括号,它就可以工作。
时间线的内容将由完全不习惯编纂标记的人来编写,即历史学家、艺术史学家、社会学家,其中有一些是50岁及以上的人。我试图向他们解释如果他们想要创建时间线,他们必须如何格式化 XML 文件,但是当时间线未加载时,他们偶尔会犯错并感到沮丧,因为他们忘记关闭括号或包含撇号。
为了使它更容易,我尝试制作一个易于使用的输入表单,您可以在其中输入所有信息并格式化文本 WYSIWYG 样式,然后将其转换为 XML 代码,您只需将其复制并粘贴到XML 源文件。尽管我仍然在主文本字段中的文本标记转换方面苦苦挣扎,但其中大部分都有效。
为了获得有效的输入表单,我需要做的最后一件事是将代码括号转换为符号代码。
I am currently trying to code an input form where you can type and format a text for later use as XML entries. In order to make the HTML code XML-readable, I have to replace the code brackets with the corresponding symbol codes, i.e. <
with <
and >
with >
.
The formatted text gets transferred as HTML code with the variable inputtext, so we have for example the text
The <b>Genji</b> and the <b>Heike</b> waged a long and bloody war.
which needs to get converted into
The <b>Genji</b> and the <b>Heike</b> waged a long and bloody war.
I tried it with the .replace() function:
inputxml = inputxml.replace("<", "<");
inputxml = inputxml.replace(">", ">");
But this would just replace the first occurrence of the brackets. I'm pretty sure I need some sort of loop for this; I also tried using the each()
function from jQuery (a friend recommended I looked at the jQuery package), but I'm still new to coding in general and I have troubles getting this to work.
How would you code a loop which would replace the code brackets within a variable as described above?
Additional information
You are, of course, right in the assumption that this is part of something larger. I am a graduate student in Japanese studies and currently, I am trying to visualize information about Japenese history in a more accessible way. For this, I am using the Simile Timeline API developed by MIT grad students. You can see a working test of a timeline on my homepage.
The Simile Timeline uses an API based on AJAX and Javascript. If you don't want to install the AJAX engine on your own server, you can implement the timeline API from the MIT. The data for the timeline is usually provided either by one or several XML files or JSON files. In my case, I use XML files; you can have a look at the XML structure in this example.
Within the timeline, there are so-called "events" on which you can click in order to reveal additional information within an info bubble popup. The text within those info bubbles originates from the XML source file. Now, if you want to do some HTML formatting within the info bubbles, you cannot use code bracket because those will just be displayed as plain text. It works if you use the symbol codes instead of the plain brackets, however.
The content for the timeline will be written by people absolutely and totally not accustomed to codified markup, i.e. historians, art historians, sociologists, among them several persons of age 50 and older. I have tried to explain to them how they have to format the XML file if they want to create a timeline, but they occasionally slip up and get frustrated when the timeline doesn't load because they forgot to close a bracket or to include an apostrophe.
In order to make it easier, I have tried making an easy-to-use input form where you can enter all the information and format the text WYSIWYG style and then have it converted into XML code which you just have to copy and paste into the XML source file. Most of it works, though I am still struggling with the conversion of the text markup in the main text field.
The conversion of the code brackets into symbol code is the last thing I needed to get working in order to have a working input form.
发布评论
评论(6)
看这里:
http://www.bradino.com/javascript/string-replace/
只需使用此正则表达式即可替换所有内容:
look here:
http://www.bradino.com/javascript/string-replace/
just use this regex to replace all:
要在 XML 中存储任意字符串,请使用浏览器的本机 XML 功能。这样会简单很多,而且您将永远不必再考虑边缘情况(例如包含引号或尖括号的属性值)。
使用 XML 时要考虑的提示:如果有任何方法可以避免的话,永远不要通过串联从字符串构建 XML。这样你将会给自己带来麻烦。有处理 XML 的 API,请使用它们。
根据您的代码,我建议如下:
To store an arbitrary string in XML, use the native XML capabilities of the browser. It will be a hell of a lot simpler that way, plus you will never have to think about the edge cases again (for example attribute values that contain quotes or pointy brackets).
A tip to think of when working with XML: Do never ever ever build XML from strings by concatenation if there is any way to avoid it. You will get yourself into trouble that way. There are APIs to handle XML, use them.
Going from your code, I would suggest the following:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects /String/replace
您可以使用带有“g”(全局匹配)标志的正则表达式。
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace
You might use a regular expression with the "g" (global match) flag.
您还可以使用以下内容包围您的 XML 条目:
请参阅示例:
维基百科文章:
http://en.wikipedia.org/wiki/CDATA
You could also surround your XML entries with the following:
See example:
Wikipedia Article:
http://en.wikipedia.org/wiki/CDATA
正如注释中提到的,您真正需要的是对字符串进行 XML 编码。如果您绝对想在 Javascript 中执行此操作,请查看 PHP.js 函数 htmlentities。
What you really need, as mentioned in comments, is to XML-encode the string. If you absolutely want to do this is Javascript, have a look at the PHP.js function htmlentities.
我创建了一个简单的 JS 函数来替换大于和小于字符
这是一个脏字符串示例: < [电子邮件受保护] >
以下是已清理字符串的示例:[ [email protected] ]
使用调用它
I created a simple JS function to replace Greater Than and Less Than characters
Here is an example dirty string: < [email protected] >
Here is an example cleaned string: [ [email protected] ]
Call it using