提取一个以"开头的特定字符串(/"以" /结尾)"在java脚本中从长字符串

时间:2017-09-06 15:42:09

标签: javascript

提取以"(/"以" /结尾)"开头的特定字符串;使用长字符串中的javascript。 我试过正规表达没有运气

我的字符串如下

  

"亲爱的Mr.M先生。 (/姓名/),继续(/ Developer /)的职位,我想询问你的招聘决定的进展情况和我的求职申请状况。我非常渴望(/工作/)与贵公司合作。感谢您的时间和考虑,我期待很快收到您的回复。真诚地,(/ YourName /)"

所需的输出=姓名,开发者,工作,你的名字

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码

let result =  "Dear Mr./Ms. (/Name/),​Following up for the position of (/Developer/), I’d like to inquire about the progress of your hiring decision and the status of my job application. I am very eager to (/Work/) with your company.​Thanks for your time and consideration, and I look forward to hear back from you soon.​Sincerely,(/YourName/)";

let ans = result.match(/\/\w+\//g);

ans = ans.map(e => e.replace(/\//g, ''));
console.log(ans);

答案 1 :(得分:0)

希望这有帮助...假设你有一个单词数组来替换所说的字符串,这应该有用......

                var testStrings = {'one':'awesome','two':'more awesome'};
                var testStr = 'I went on an /one/ walk and found a dollar on the street /two/!';
                var newStr = testStr;
                $.each(testStrings,function(k,v){
                    console.log('k',k);
                    console.log('v',v);
                    newStr = newStr.replace('/'+k+'/',v);
                });
                console.log('testStr',testStr);
                console.log('newStr',newStr);

初始字符串: “我去了一个/一个/步行,在街上找到一块钱/两个/!”

输出STring: “你的输出将是我走了一个很棒的步行,发现街上的一块钱更加棒!”