简体   繁体   English

在javascript中删除除字符串的特定部分之外的所有部分

[英]remove all but a specific portion of a string in javascript

I am writing a little app for Sharepoint. 我正在为Sharepoint写一个小应用程序。 I am trying to extract some text from the middle of a field that is returned: 我试图从返回的字段的中间提取一些文本:

var ows_MetaInfo="1;#Subject:SW|NameOfADocument
vti_parservers:SR|23.0.0.6421
ContentTypeID:SW|0x0101001DB26Cf25E4F31488B7333256A77D2CA
vti_cachedtitle:SR|NameOfADocument
vti_title:SR|ATitleOfADocument
_Author:SW:|TheNameOfOurCompany
_Category:SW|
ContentType:SW|Document
vti_author::SR|mrwienerdog
_Comments:SW|This is very much the string I need extracted
vti_categories:VW|
vtiapprovallevel:SR|
vti_modifiedby:SR|mrwienerdog
vti_assignedto:SR|
Keywords:SW|Project Name
ContentType _Comments"

So......All I want returned is "This is very much the string I need extracted" 所以......我想要的只是“这是我需要提取的字符串”

Do I need a regex and a string replace? 我需要正则表达式和字符串替换吗? How would you write the regex? 你会怎么写正则表达式?

Yes, you can use a regular expression for this (this is the sort of thing they are good for). 是的,您可以使用正则表达式(这是他们喜欢的东西)。 Assuming you always want the string after the pipe (|) on the line starting with "_Comments:SW|", here's how you can extract it: 假设你总是想要以“_Comments:SW |”开头的行(|)之后的字符串,这里是你如何提取它:

var matchresult = ows_MetaInfo.match(/^_Comments:SW\|(.*)$/m);
var comment = (matchresult==null) ? "" : matchresult[1];

Note that the .match() method of the String object returns an array. 请注意,String对象的.match()方法返回一个数组。 The first (index 0) element will be the entire match (here, we the entire match is the whole line, as we anchored it with ^ and $; note that adding the "m" after the regex makes this a multiline regex, allowing us to match the start and end of any line within the multi-line input), and the rest of the array are the submatches that we capture using parenthesis. 第一个(索引0)元素将是整个匹配(这里,我们整个匹配是整行,因为我们用^和$锚定它;注意在正则表达式之后添加“m”使得它成为多行正则表达式,允许我们匹配多行输入中任何行的开始和结束),数组的其余部分是我们使用括号捕获的子匹配。 Above we've captured the part of the line that you want, so that will present in the second item in the array (index 1). 上面我们已经捕获了你想要的那一行,所以它将出现在数组的第二个项目中(索引1)。

If there is no match ("_Comments:SW|" doesnt appear in ows_MetaInfo), then .match() will return null, which is why we test it before pulling out the comment. 如果没有匹配(“_Comments:SW |”没有出现在ows_MetaInfo中),那么.match()将返回null,这就是我们在撤出注释之前测试它的原因。

If you need to adjust the regex for other scenarios, have a look at the Regex docs on Mozilla Dev Network: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions 如果您需要针对其他方案调整正则表达式,请查看Mozilla Dev Network上的Regex文档: https//developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions

You can use this code: 您可以使用此代码:

var match = ows_MetaInfo.match(/_Comments:SW\|([^\n]+)/);
if (match)
   document.writeln(match[1]);

I'm far from competent with RegEx, so here is my RegEx-less solution. 我对RegEx不太称职,所以这是我的RegEx-less解决方案。 See comments for further detail. 有关详细信息,请参阅注释

var extractedText = ExtractText(ows_MetaInfo);

function ExtractText(arg) {
    // Use the pipe delimiter to turn the string into an array
    var aryValues = ows_MetaInfo.split("|");

    // Find the portion of the array that contains "vti_categories:VW"
    for (var i = 0; i < aryValues.length; i++) {
        if (aryValues[i].search("vti_categories:VW") != -1)
            return aryValues[i].replace("vti_categories:VW", "");
    }

    return null;
}​

Here's a working fiddle to demonstrate . 这是一个工作小提琴演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM