簡體   English   中英

如何獲取從函數返回的值並將其用於另一個函數

[英]how to get the value returned from the function and use it another function

我正在嘗試使用 javascript 中的正則表達式來創建一個用於清理字段的函數。 這是我的職能。 我的最終目標是獲取一個字符串並對其進行消毒並在某處重復使用。 就像我這個場景我需要字符串

function sani(str){
    str = str.replace(/[^a-zA-Z ]/g, "");
    return str;
}

$('button').click(function(){
    var word = "<h1>Hello W<o>rld";
    alert(str);
    cleanStr(str);
    //I need the following alert to be the string after sanitisation
    alert("After sanitisation : "+str)
})

假設您想調用sani而不是cleanStr您可以執行以下操作:

function sani(str){
    str = str.replace(/[^a-zA-Z ]/g, "");
    return str;
}

$('button').click(function(){
    var word = "<h1>Hello W<o>rld";
    alert(word);
    var result = sani(word);
    //I need the following alert to be the string after sanitisation
    alert("After sanitisation : " + result)
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM