javascript 如果条件检查字符串带有换行符,即 \n
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19703347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript if condition check string with newline symbol i.e \n
提问by aarav
In my script I have a string with newline characters. Using newline (\n
)character I parsed it to array.
在我的脚本中,我有一个带有换行符的字符串。使用换行符 ( \n
) 字符我将其解析为数组。
I struggling to compare in my if condition (parsedarray[0]=='newline character'
). Please help me to compare the newline character in my if condition. I also tried to alert the parsedarray[0]
. It alerts blank alert box.` I am unable to check the newline character in if statement. For instance,I have a single String with multiple newline , tabs consisting of 40 lines. In my script user enter a line number, string for that line number, after receiving both information, I want to replace the newline with entered string in the line number. Here line number is an index. So that again I will construct a single string by joining the parsed string. Also my array size should not grow. or Reduce.??? And importantly I want to validate the given String with the available string, if both matches (other than newline) need to put alert message.
我努力在我的 if 条件 ( parsedarray[0]=='newline character'
) 中进行比较。请帮助我比较 if 条件中的换行符。我也试图提醒parsedarray[0]
. 它会提醒空白警报框。` 我无法检查 if 语句中的换行符。例如,我有一个带有多个换行符的 String ,由 40 行组成的选项卡。在我的脚本中,用户输入一个行号,该行号的字符串,在收到这两个信息后,我想用行号中输入的字符串替换换行符。这里行号是一个索引. 因此,我将再次通过连接解析的字符串来构造单个字符串。我的数组大小也不应该增长。或减少。???重要的是,如果两个匹配项(换行符除外)都需要放置警报消息,我想用可用的字符串验证给定的字符串。
var strarray=doc.getElementbyid('mytextarea').value;
var parsedarray=[];
parsedarray=strarray.split('\n');`
回答by Joke_Sense10
Instead of split
use indexOf
to find position of the first occurrence of a specified value in a string.
而不是split
使用indexOf
查找字符串中的规定值的第一次出现的位置。
Eg:
例如:
var text="Hello world\n";
if(text.indexOf("\n")==-1){
alert("No newline characters")
}else{
alert("Contains newline characters")
}
回答by Daniel Perván
String.split() will not include the delimiter in the resulting array. If you want to find the occurrence of a newline character, I would suggest using String.indexOf("\n")
instead.
String.split() 不会在结果数组中包含分隔符。如果你想找到换行符的出现,我建议String.indexOf("\n")
改用。
回答by Bharath R
You can do something like this
你可以做这样的事情
<html>
<head>
<script>
function myFunction()
{
var strarray=document.getElementById("tre").value;
var eachLine = strarray.split('\n');
alert('Lines found: ' + eachLine.length);
for(var i = 0, l = eachLine.length; i < l; i++) {
//do add another string to the index here
alert('Line ' + (i+1) + ': ' + eachLine[i]);
}
</script>
</head>
<body>
<textarea id="tre"></textarea>
<button onclick="myFunction()">Click me</button>
</body>
</html>
回答by Guilherme Sehn
The split
method will split your string into an array of strings that were separated by the character you passed as parameter. For example:
该split
方法会将您的字符串拆分为由您作为参数传递的字符分隔的字符串数组。例如:
var text = 'foo\nbar\nbaz',
splitted = text.split('\n');
console.log(splitted); // ["foo", "bar", "baz"]
If you want to replace your new line character with another string, you can use the replace
method.
如果您想用另一个字符串替换换行符,可以使用该replace
方法。
var text = 'foo\nbar\nbaz',
replaced = text.replace(/\n/g, '-');
console.log(replaced); // foo-bar-baz
回答by aarav
I recreated the array like adding a delimiter var one=document.forms[0]["mytextarea"].value;
myarray=one.split('\|');
after each newline character. Then I split the string using the delimiter. Then onwards I can easily accessing the newline \n
character by each index. After replacing the string on specific index, again i construct a single string by joining them using same delimiter. var two=myarray.join('|');
Also my alertbox shows now \n
, when I tried to print the index. Thanks for all your contribution..
我重新创建了数组,就像var one=document.forms[0]["mytextarea"].value;
myarray=one.split('\|');
在每个换行符之后添加一个分隔符一样。然后我使用分隔符拆分字符串。然后我可以\n
通过每个索引轻松访问换行符。替换特定索引上的字符串后,我再次通过使用相同的分隔符连接它们来构造单个字符串。当我尝试打印索引时,var two=myarray.join('|');
我的警报框现在显示\n
。感谢您的所有贡献..