我试图从一个更大的字符串中提取一个字符串,它得到了a:和a之间的所有东西;

当前的

Str = 'MyLongString:StringIWant;'

期望输出值

newStr = 'StringIWant'

当前回答

上面的代码适用于简单的例子,但可以帮助… 使用打印稿。

参数

句子:你想要得到部分的字符串 第一个:开头字符(对于初始示例,它将是:) 最后:你的部分的最后一个字符(对于初始示例,它将是;)

输出

字符串数组(string[])。如果句子中没有好的部分,则返回[]。

Code

function getParts(sentence: string, first: string, last: string): string[] { 
  let goodParts: string[] = [];
  
  const allParts = sentence.split(first);

  allParts.forEach((part: string) => {
    if (part.indexOf(last) > -1) {
            const goodOne = (part.split(last))[0];
      goodParts = goodParts.concat(goodOne);
    }
  });
  
  return goodParts;
}

例子

const origin = "wrongString1:rightString1;wrongString2:rightString2;wrongString3:rightString3;wrongString4:rightString4;";

const result = getParts(origin, ':', ';');

console.log(result);
// ["rightString1", "rightString2", "rightString3", "rightString4"]

其他回答

你也可以用这个…

function extractText(str,delimiter){ if (str && delimiter){ var firstIndex = str.indexOf(delimiter)+1; var lastIndex = str.lastIndexOf(delimiter); str = str.substring(firstIndex,lastIndex); } return str; } var quotes = document.getElementById("quotes"); // &#34 - represents quotation mark in HTML <div> <div> <span id="at"> My string is @between@ the "at" sign </span> <button onclick="document.getElementById('at').innerText = extractText(document.getElementById('at').innerText,'@')">Click</button> </div> <div> <span id="quotes"> My string is "between" quotes chars </span> <button onclick="document.getElementById('quotes').innerText = extractText(document.getElementById('quotes').innerText,'&#34')">Click</button> </div> </div>

你可以使用更高阶的函数来返回你的提取器的“编译”版本,这样更快。

使用正则表达式,并在闭包中编译一次正则表达式,Javascript的匹配将返回所有匹配项。

这让我们只需要删除我们用作标记的东西(例如:{{),我们可以使用字符串长度来实现slice。

function extract([beg, end]) {
    const matcher = new RegExp(`${beg}(.*?)${end}`,'gm');
    const normalise = (str) => str.slice(beg.length,end.length*-1);
    return function(str) {
        return str.match(matcher).map(normalise);
    }
}

编译一次,使用多次…

const stringExtractor = extract(['{','}']);
const stuffIneed = stringExtractor('this {is} some {text} that can be {extracted} with a {reusable} function');
// Outputs: [ 'is', 'text', 'extracted', 'reusable' ]

或者一次性使用……

const stuffIneed = extract(['{','}'])('this {is} some {text} that can be {extracted} with a {reusable} function');
// Outputs: [ 'is', 'text', 'extracted', 'reusable' ]

也可以看看Javascript的replace函数,但使用了替换参数的函数(如果你在做一个迷你模板引擎(字符串插值),你会这样做……lodash。Get也可以帮助你得到你想要替换的值?...

我的回答太长了,但它可能会帮助到一些人!

这可能是可行的解决方案

var str = 'RACK NO:Stock;PRODUCT TYPE:Stock Sale;PART N0:0035719061;INDEX NO:21A627 042;PART NAME:SPRING;';  
var newstr = str.split(':')[1].split(';')[0]; // return value as 'Stock'

console.log('stringvalue',newstr)

使用' get_between '实用函数:

get_between <- function(str, first_character, last_character) {
    new_str = str.match(first_character + "(.*)" + last_character)[1].trim()
    return(new_str)
    }

字符串

my_string = 'and the thing that ! on the @ with the ^^ goes now' 

用法:

get_between(my_string, 'that', 'now')

结果:

"! on the @ with the ^^ goes

获取所有子字符串。

var out = []; 'MyLongString:StringIWant;'
.replace(/(:)\w+(;)+/g, (e) => {
    out.push(e.replace(':', '').replace(';', ''))
    return e;
});
console.log(out[0])