2017-09-06 9 views
0

javascriptを使用して "/"で始まる特定の文字列を抽出します。長い文字列。 は、私は正規表現に運を試してみませんJavaスクリプトの長い文字列から "(/"で始まり、 "/"で始まる)特定の文字列を抽出します。

私の文字列

「親愛なるMr./Msを次のように。(/名前/)、(/開発/)の位置のためのフォローアップ、私は思いますあなたの雇用決定の進捗状況と私の就職状況についてお尋ねしたいと思います。私はあなたの会社と非常に熱心です(/働く/)あなたの時間と配慮をお寄せいただきありがとうございます。敬具(/ YourName /)」

希望の出力=名前、開発者、仕事、YourName

+0

私たちにあなたが試したものとあなたが立ち往生した場所を教えてください。 –

+0

const regex =/[(//)]/g; const str = "mystring"; mとします。 一方((M = regex.exec(STR))!== NULL){ //これはゼロ幅の無限ループを回避する必要がある と一致する場合(m.index === regex.lastIndex){ regex.lastIndex ++; } //結果は 'm'変数を通してアクセスできます。 (一致、グループ$ {グループ一致}:$ {一致} '); )); } –

+0

['String.prototype.match()'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match)を勉強することをお勧めします – Redu

答えて

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);

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); 

最初の文字列: '私は/ one/walkに行き、通りにドルを見つけました/ 2 /!'

出力STring: 'あなたの出力はすばらしい散歩になり、通りにはさらに素晴らしいものが見つかりました!'

関連する問題