[英]Dynamic str replace with javascript on html containing stream
I have a html which looks roughly like this 我有一个看起来像这样的html
<div id="wallPostTemplate">
<li id='comment-content--oOo-id-oOo-' class='comment-content' comment_id='-oOo-id-oOo-'>
<a class='comment-avatar' href='-oOo-link-oOo-'>
<img class='small_avatar' src='<?="../" . MESTO_ZA_UPLOAD_FAJLOVA_KORISNIKA ?>-oOo-korisnikPodaci.Slika-oOo-'></a>
-oOo-text-oOo-
</li>
</div>
Now I get the contents of the html into a sting with $('#wallPostTemplate').text()
and then I want to replace dynamically the occurrences of the string -oOo-id-oOo-
with whatever data I have stored inisde a variable like -oOo-id-oOo-
, -oOo-text-oOo-
and so on... 现在,我将html的内容放入
$('#wallPostTemplate').text()
的字符串中,然后我想用我存储的任何数据动态替换字符串-oOo-id-oOo-
oOo-的出现变量-oOo-id-oOo-
,- -oOo-text-oOo-
等...
Now I tried numerous ways to tackle this thing but I always end up without a solution. 现在,我尝试了多种方法来解决此问题,但最终始终没有解决方案。 Here is my latest try out.
这是我最新的尝试。 HELP pls Q_Q
帮助请Q_Q
var regex = new RegExp('{\-oOo\-'+index+'\-oOo\-}', "g");
post = post.replace(regex, value);
Also I tried str.replace
but it does not work... It seems new lines are the problem but I have no idea how to over come this..... 我也尝试了
str.replace
但是它不起作用...似乎str.replace
行了,但是我不知道如何克服这个问题.....
edit: 编辑:
To make it more simple to understand i made a example on jsfidle: http://jsfiddle.net/DdBFB/ And as you can see, you can see nothing.... it dous not work when you get the content over the html function. 为了更容易理解,我在jsfidle上举了一个例子: http : //jsfiddle.net/DdBFB/正如您所看到的,您什么也看不到...。当您通过html获取内容时,它确实无法工作功能。
If I understand your question your goal is make something like a post template that will be loaded in runtime/cliente side. 如果我理解您的问题,您的目标是制作类似发布模板的内容,该模板将在运行时/客户端中加载。 So if this is really your goal you can try use the jQuery Template Plugin
因此,如果这真的是您的目标,则可以尝试使用jQuery Template Plugin
Sample for your case: (don´t tested yet) 您的案例样本:(尚未测试)
var tmlpPost = "<div id='${postId}'>" +
" <li id='comment-content-${postId}' class='comment-content' comment_id='${postId}'>" +
" <a class='comment-avatar' href='${link}'>" +
" img class='small_avatar' src='<?="../" . MESTO_ZA_UPLOAD_FAJLOVA_KORISNIKA ?>${korisnikPodaciSlika}'>" +
" </a> " +
" ${postText}" +
" </li>" +
"</div>";
$.tmpl( tmlpPost, { "postId" : "123" , "link" : "http://posturl.com", "korisnikPodaciSlika" : "something", "postText" : "Post text goes here..."}).appendTo( "#yourPostWall" );
Or using <script>
not a var
: See this Fiddle 或使用
<script>
而不是var
: 请参阅此提琴
<script id="tmlpPost" type="text/x-jquery-tmpl">
<div id='${postId}'>
<li id='comment-content-${postId}' class='comment-content' comment_id='${postId}'>
<a class='comment-avatar' href='${link}'>
<img class='small_avatar' src='<?="../" . MESTO_ZA_UPLOAD_FAJLOVA_KORISNIKA ?>${korisnikPodaciSlika}'>
</a>
${postText}
</li>
</div>
</script>
Now on page load: 现在页面加载:
$("#tmlpPost").tmpl( { "postId" : "123" , "link" : "http://posturl.com", "korisnikPodaciSlika" : "something", "postText" : "Post text goes here..."}).appendTo( "#output" );
Although your question is not really clear I've whipped up an example that replaces a value based on a regular expression. 尽管您的问题还不清楚,但我还是举了一个示例,该示例替换基于正则表达式的值。
var source = "-oOo-korisnikPodaci.Slika-oOo-";
var id = "korisnikPodaci.Slika";
var regex = new RegExp('\-oOo\-' + id + '\-oOo\-', "gmi");
// Match
source.replace(regex, "hello world"); // => "Hello World"
// Mismatch
source = "-oO!!!o-korisnikPodaci.Slika-oO!!!o-";
source.replace(regex, "hello world"); // => "-oO!!!o-korisnikPodaci.Slika-oO!!!o-"
ok, i figured the answer was that i set the regex flags wrong, so i went to jsfiddle and fiddled with it and you can see the answer there :) 好的,我认为答案是我设置了正则表达式标志错误,所以我去了jsfiddle并弄弄了它,您可以在那看到答案了:)
http://jsfiddle.net/DdBFB/11/ http://jsfiddle.net/DdBFB/11/
I was missing the multi line flag Q_Q....im no good with regex even in php not to speak in js... 我错过了多行标志Q_Q ....即使在php中也不能在js中使用正则表达式也不行...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.