[英]Extracting a part of simple string using Javascript
Let's say I have these strings (each new line being a separate string): 假设我有以下字符串(每行都是一个单独的字符串):
EducationLink
BioLink
InterestsLink
And I wanted to extract the part that's not "Link" using Javascript. 我想使用Javascript提取不属于“链接”的部分。 How would I do this?
我该怎么做? The expected results are
预期结果是
Education
Bio
Interests
I tried a Regex, but I'm not very experienced with them, and it failed: 我尝试过一个正则表达式,但是对它们的经验不是很丰富,但是失败了:
/^ (^Link) $/
Using string functions such as slice() and substr(), I only got the values to the right of the selected text, not to the left as desired. 使用诸如slice()和substr()之类的字符串函数,我只在所选文本的右侧获取了值,而不是所需的左侧。
Thanks for your help. 谢谢你的帮助。
text = input.replace(/Link(\n|$)/g,"\n");
you can use .replace()
您可以使用
.replace()
'EducationLink'.replace('Link', ''); //returns Education
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace https://developer.mozilla.org/zh-CN/JavaScript/Reference/Global_Objects/String/replace
The thing is you CAN use regex to do this replace, but being such a simple scenario (unless there is more to it) why would you overcomplicate things? 问题是您可以使用正则表达式进行替换,但是由于情况如此简单(除非有更多情况),您为什么会使事情过于复杂?
You would need to put the Link
into the end of your RegEx, such as: 您需要将
Link
放在RegEx的末尾,例如:
^([A-Z]+)Link$
This matches: 这符合:
Link
Link
The part in parentheses will be returned as a group. 括号中的部分将作为一组返回。
Furthermore, if all you need to do is remove the suffix Link
, you can use the replace()
method in Javascript: 此外,如果只需删除后缀
Link
,则可以在Javascript中使用replace()
方法:
var x = myStr.replace('Link', '');
一种方法是使用String.split() MDN
var txt = 'EducationLink'.split('Link')[0]; // txt == 'Education'
Try This: 尝试这个:
<script>
var str = "EducationLink";
index = str.search(/link/i);
str = str.substr(0,index);
</script>
As others have pointed out, if all the strings end in 'Link', just use a replace. 正如其他人指出的那样,如果所有字符串都以“ Link”结尾,则只需使用替换即可。 Otherwise, any of these methods will work fine:
否则,这些方法中的任何一种都可以正常工作:
var i = 0;
var strings = [
'EducationLink',
'BioLink',
'InterestsLink'
];
var s = '';
var r = /^(.+)Link$/
for (i = 0; i < strings.length; i += 1) {
s = strings[i];
//s = s.substring(0, s.indexOf('Link')); //Uncomment for substring
//s = s.match(r)[1]; //Uncomment for regex
s = s.replace('Link', '');
strings[i] = s;
}
console.log(strings);
Copy and paste into a browser console window to run. 复制并粘贴到浏览器控制台窗口中以运行。
To answer your question ("However, why did my RegEx not work?") in a comment on another answer, your RegEx does the following: 要在对另一个答案的评论中回答您的问题(“但是,我的RegEx为什么不起作用?”),您的RegEx会执行以下操作:
/^ (^Link) $/
//Each regex term on own line, commented below
^ //start of string
//Literal space
(^Link) //Capturing group, string starting with 'Link'.
//Literal space
$ //end of string
While you can have two "start of string" identifiers in a Regex, there will only ever be one "start of string". 尽管在正则表达式中可以有两个“字符串开头”标识符,但永远只有一个“字符串开头”。 The
^
character is only a negation in a character class which is enclosed in square brackets, not curved. ^
字符只是字符类中的否定词,该字符类括在方括号中,而不是弯曲的。 If you want to capture a literal caret ( ^
), put a backslash in front of it ( \\^
). 如果要捕获文字插入符(
^
),请在其前面加一个反斜杠( \\^
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.