2017-10-06 24 views
0

次のコードexamines the content of inputData.body for a 32-character string matchと、わかっている限り、すべての一致が配列に配置されます。正規表現が一致しない場合は "false"を返す

// stores an array of any length (0 or more) with the matches 
var matches = inputData.body.match(/\b[\w-]{32}\b/g) 

// the .map function executes the nameless inner function once for each element of the array and returns a new array with the results 
return matches.map(function (m) { return {str: m} }) 

私は今、例えば、無マッチした表現の場合には何かを返すようにコードが必要です。文字列"false"

私はどのように私は条件付きで空虚のイベントで何かを返すに取り掛かる必要があります...

// stores an array of any length (0 or more) with the matches 
var matches = inputData.body.match(/\b[\w-]{32}\b/g) 

if (matches == null){ 
    return 'false' 
} 

// the .map function executes the nameless inner function once for each element of the array and returns a new array with the results 
return matches.map(function (m) { return {str: m} }) 

このほか作業を取得することができませんでしたか?

+0

コードは機能的にラップされていますか? –

+2

私はあなたのコードが動作するはずだと思います。 – Barmar

+0

呼び出し元が関数の結果として文字列を取得する準備ができていますか?おそらく配列が必要です。 – Barmar

答えて

0

呼び出し元は、オブジェクトの配列または単一のオブジェクト(おそらくその1つのオブジェクトの配列のように扱われる)が必要です。したがって、単一のオブジェクトを返します。

if (matches == null) { 
    return { str: "false"; } 
} 
return matches.map(function (m) { return {str: m} }); 

または単一のステートメントで:

return matches == null ? { str: "false"; } : matches.map(function (m) { return {str: m} }); 
+0

です。空文字を埋めるのではなく、何らかの肯定的な出力が必要です。上記の 'false 'で' str'を書き込むと、それが行われているように見え、後のステップで 'false'という単語をテストすることができます。かわいいですが、私が必要とするものです。私はこれを条件付けする必要があると想像しています......... 'if(matches == null){ return {str: 'false'}; } else { matches.map(function(m){return {str:m}}) } '? –

+0

'if'が' return'を実行する場合、 'else'は必要ありません。 – Barmar

+1

私はドキュメントを見て、それは空の配列がOKであると言うようです。 ** ZapierによるコードがZapのトリガーで空の配列が返された場合、何も起こらない** – Barmar

0

呼び出し側は、おそらく何の一致がなかった場合は空のものでなければならないの配列を、期待しています。正規表現は、あなたが偽/ trueを返すtest()メソッドを使用する必要がある特定のパターンに一致した場合

return (matches || []).map(function (m) { return {str: m} }) 
関連する問題