简体   繁体   English

在javascript字符串中用escape替换字符

[英]replacing characters with escape in a javascript string

i know there must be thousands of threads like this, but the internet and stack overflow is flooded with results from other programming languages and simply replacing the characters with a whitespace and so on... 我知道必须有成千上万的这样的线程,但互联网和堆栈溢出充斥着其他编程语言的结果,只需用空格替换字符等等......

so... my problem is as follows... 所以...我的问题如下......

i have a form with: <input type='text' id='my_input'> 我有一个表单: <input type='text' id='my_input'>

then i parse all my fields in javascript (with jquery) to create my ajax call, like this 然后我用javascript(用jquery)解析我的所有字段来创建我的ajax调用,就像这样

var my_value = $('#my_input').val();

then i do a replace to get the line breaks my_value = my_value.replace('\\n', '<br>'); 然后我做一个替换来获得换行符my_value = my_value.replace('\\n', '<br>');

after that i post my variables with ajax like this: 之后,我用ajax发布我的变量,如下所示:

data_to_post = 'my_field1=' + my_value1 + 'my_field2' + my_value2;

$.ajax({  
type: 'POST',  
url: '/write_to_db.php',  
data: data_to_post
});  

now many people on my site talk about programming and that content started screwing up my site, because their post content gets parsed as php or as html formatting - i'm planning to replace all the problematic characters that could screw up the code of my site. 现在很多人在我的网站上谈论编程和内容开始搞砸了我的网站,因为他们的帖子内容被解析为php或html格式 - 我打算替换所有有问题的字符,可能搞砸了我的网站的代码。

the most problematic expressions are html tags and &= because it interferes with the way i put my post variable together. 最有问题的表达式是html标签和&=因为它干扰了我将post变量放在一起的方式。

now, because those characters are necessary for the content, i can't simply replace them all with with a single character. 现在,因为这些字符是内容所必需的,所以我不能简单地用一个字符替换它们。

is there any way i could escape them (at best in a one line command)? 有什么方法可以逃脱它们(最好是在一行命令中)?

thanks! 谢谢!

UPDATE: my temporary solution is this: 更新:我的临时解决方案是:

this_string = this_string.replace(/</g, '&#60;');
this_string = this_string.replace(/>/g, '&#62;');
this_string = this_string.replace(/\n/g, '<br>');
this_string = escape(this_string);

I would use $.serializeJSON() on the form element itself to gather up all the values for the entire form. 我会在表单元素本身上使用$ .serializeJSON()来收集整个表单的所有值。 Then I would send that to the server-side script via an AJAX call. 然后我会通过AJAX调用将其发送到服务器端脚本。 A task like parsing a complex comment or text field that allows for code as well as plain text is better left to a server-side language. 解析复杂注释或文本字段(允许代码和纯文本)这样的任务最好留给服务器端语言。

try the javascript function escape(): http://www.w3schools.com/jsref/jsref_escape.asp 尝试javascript函数escape(): http//www.w3schools.com/jsref/jsref_escape.asp

data_to_post = 'my_field1=' + escape(my_value1) + 'my_field2' + escape(my_value2);

Edit: 编辑:

to deal with the HTML characters the people are not allowed to post, you could php this: 要处理人们不允许发布的HTML字符,你可以用php:

$outputstring = "hi<br />bye!";
echo htmlspecialchars($outputstring);

info on how this works: http://php.net/manual/en/function.htmlspecialchars.php 有关如何工作的信息: http//php.net/manual/en/function.htmlspecialchars.php

How about: 怎么样:

my_value = my_value.replace(/\n|\.|\~/g, '<br>');

Put your characters to replace around the | 把你的角色替换为| (OR) separator and have the /g flag at the end (或)分隔符,并在末尾有/ g标志

Although your intent is not entirely clear to me, you could preserve the programmers' content and still display the carriage return/linefeed by wrapping the expression in a tag. 尽管您的意图并不完全清楚,但您可以保留程序员的内容,并通过将表达式包装在标记中来显示回车符/换行符。

Another possibility is to use CSS to style the appearance. 另一种可能性是使用CSS来设计外观样式。 Take a look at the answer to this post: How can I replace certain carriage return line feed followed by a dash with a <br/>? 看一下这篇文章的答案: 如何更换某些回车换行符,然后换一个带有字母的短划线?

It sounds like you are simply wanting to encode the HTML characters to preserve the HTML characters, so if this is the case the following will encode the text correctly: 听起来您只是想编码HTML字符以保留HTML字符,因此如果是这种情况,以下内容将正确编码文本:

var textVal = $('#my_input').val();
var encoded = $('<div/>').text(textVal).html();  

This creates an in-memory div to help with encoding. 这会创建一个内存中的div来帮助编码。 To decode you can simply change the div call to: 要解码,您只需将div调用更改为:

  var unencoded = $('<div/>').html(textVal).text()

您是否考虑过在以下示例中为您处理PHP?

nl2br(htmlspecialchars($str,ENT_QUOTES,'UTF-8'));
$.ajax({  
type: 'POST',  
url: '/write_to_db.php',  
data: {
   'my_field1': my_value1,
   'my_field2': my_value2
}
}); 

Pass an object in the data instead of a string, jquery do that for you. 传递数据中的对象而不是字符串,jquery为您执行此操作。

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

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