JavaScript - 获取从位置到行尾的字符串/return 字符

JavaScript - Get string from position till end of line / return character

我有一个字符串,它被压缩成一行,带有一个 (return?) 字符,但应该是这样的:

Service Type: Registration Coordinator

Authorized Amount: 
Authorized Mileage: .25

我想得到 'Registration Coordinator' 或直到行尾的任何内容,所以我尝试了这个:

var service_type = t_var.description.substr(t_var.description.indexOf('Service Type: ') +14,t_var.description.indexOf('\n'));

但是 returns:

Registration Coordinator

Authorized A

这是原始字符串在发布到数据库之前的创建方式,我正在尝试在从数据库读回后使用它:

var fullDescription = "Service Type: " + that.current_wo_data.service_partner.skill + "\n\n";
fullDescription += '\nAuthorized Amount: $' + that.$('#authorized_amount').val();
fullDescription += '\nAuthorized Mileage: $' + that.$('#authorized_mileage').val();

谢谢

substrsubstring有区别(不要问我为什么)

.substr(start_pos, number_of_chars);
"abcdef".substr(2, 3) returns "cde"
.substring(start_pos, end_pos);
"abcdef".substr(2, 3) returns "c"

您正在使用 start_posend_pos 以及函数 substr。那是自找麻烦 ;)

var service_type = t_var.description.substr(t_var.description.indexOf('Service Type: ') +14,t_var.description.indexOf('\n'));

应该是

var service_type = t_var.description.substring(t_var.description.indexOf('Service Type: ') +14,t_var.description.indexOf('\n'));