使用replace()函数时,Jquery代码会中断

时间:2015-11-16 17:14:08

标签: javascript jquery html replace

我有一个简单的代码,它使用":"将书名与序列分开。作为分隔符。这确实是一个直接的解决方案,应该可行。在我的网站上它可以工作,但在书名中使用德语变音符号时会停止。因此,我创建了一个 JSFiddle 来查找正在进行的操作,但只要我使用import java.io.IOException; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; public class Test { public static void main(String[] args) { String json = "{ \"container\": { \"containerProperty\": \"red\", \"items\": [ {\"color\": \"white\",\"shape\": \"round\"},{\"container\": {\"containerProperty\": \"blue\",\"items\": [ {\"color\": \"green\", \"shape\": \"round\" }, { \"color\": \"black\", \"shape\": \"square\" } ] } }, { \"color\": \"yellow\", \"shape\": \"round\" } ] } }"; ObjectMapper mapper = new ObjectMapper(); NestedObject nestedObj = null; try { nestedObj = mapper.readValue(json, NestedObject.class); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(nestedObj.toString()); } } 函数,代码就会中断。

HTML:

replace()

JS:

<div id="title">Die Träne des Fressers: Weiße Königin</div>
Title:<div id="output_title">output title</div>
Serial:<div id="output_serial">output serial</div>

我不明白发生了什么。有人可以向我解释一下吗?

3 个答案:

答案 0 :(得分:3)

问题是.match()正在返回一个数组 而不是 一个字符串,正如您所期望的那样。在调用.replace()之前,您需要检查并选择匹配的字符串(如果适用)。

目前通过添加一些日志记录:

title_input = $("#title").text(); 

title   = title_input.match(/[a-z\s,äöüß-]*.*:/i);
serial  = title_input.match(/:\s?[a-z\s,äöüß-]*/i);
console.log(title.length); // <-- Logging

我们可以看到title如下:

["Die Träne des Fressers:", index: 0, input: "Die Träne des Fressers: Weiße Königin"]

要使代码生效,您需要在调用.replace()之前获取title数组的第一个元素(如果可用):

&#13;
&#13;
title_input = $("#title").text(); 

title 	= title_input.match(/[a-z\s,äöüß-]*.*:/i);
serial 	= title_input.match(/:\s?[a-z\s,äöüß-]*/i);

if(title && title.length) {
  title = title[0];  
  title.replace(/\:/g,'');
}

$("#output_title").text(title);
$("#output_serial").text(serial);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<div id="title">Die Träne des Fressers: Weiße Königin</div>
Title:<div id="output_title">output title</div>
Serial:<div id="output_serial">output serial</div>
&#13;
&#13;
&#13;

额外注意:为确保您不会收到undefined类型错误,最好确保title存在且不存在if(title && title.length)行所指出的空数组。

答案 1 :(得分:2)

与评论中提到的@Pointy一样, match() 会返回一个数组,数组的第一个元素(元素0)将包含匹配的子字符串,所以你必须通过addinng [0]选择第一个元素:

title   = title_input.match(/[a-z\s,äöüß-]*.*:/i)[0];
serial  = title_input.match(/:\s?[a-z\s,äöüß-]*/i)[0];

replace() 方法也会返回替换后生成的新字符串,因此您必须将其分配给您的变量:

title = title.replace(/\:/g,'');

希望这会有所帮助。

<强> Updated fiddle

答案 2 :(得分:1)

变量标题仅在脚本可以找到其中一个符号并且标题不是字符串 - 它的数组时才存在。你不能在null或数组上使用replace。

这应该有效:

var title = $("#title").text(); 
title = title.replace(/[a-z\s,äöüß-]*.*:/i,'');

如果你想分割这个字符串,你应该使用.split(&#39;:&#39;)函数。