Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过调用自定义函数将var转换为子字符串来替换字符串-algojs_Javascript_Regex_String_Algorithm_Typescript - Fatal编程技术网

Javascript 通过调用自定义函数将var转换为子字符串来替换字符串-algojs

Javascript 通过调用自定义函数将var转换为子字符串来替换字符串-algojs,javascript,regex,string,algorithm,typescript,Javascript,Regex,String,Algorithm,Typescript,我正在尝试转换一个字符串,以使用变量和参数构建它 The min length of __40 is 4. Max length is 16. __40是另一个字符串键,我需要从字符串中提取键,将其转换并替换为转换后的值(这将是键引用的字符串),为此,我需要调用一个自定义函数[_GetSession()],在同一个句子中可能有多个键,我曾想过制作一个正则表达式,但它看起来很复杂,不是吗?另外,我不知道是否可以从正则表达式调用自定义函数,但我不这么认为 可能拆分字符串以获取所有单词(按空格拆分)

我正在尝试转换一个字符串,以使用变量和参数构建它

The min length of __40 is 4. Max length is 16.
__40是另一个字符串键,我需要从字符串中提取键,将其转换并替换为转换后的值(这将是键引用的字符串),为此,我需要调用一个自定义函数[_GetSession()],在同一个句子中可能有多个键,我曾想过制作一个正则表达式,但它看起来很复杂,不是吗?另外,我不知道是否可以从正则表达式调用自定义函数,但我不这么认为

可能拆分字符串以获取所有单词(按空格拆分),每次检查单词是否以“\uuuuu”开头,在本例中调用我的自定义函数,存储结果并用字符串替换当前单词。但是这个新字符串还可能包含另一个我需要转换的键

这里使用的最佳算法是什么?你知道有没有更好的解决办法吗?我的应该有用

我还必须处理可以发送的参数,但我不必管理子键的参数

这是一个函数,在句子转换后由_get句子()调用,用值替换args,但我认为在这里也可以管理子键

/**
 * Replace the tags in the text by the args.
 * @param message      The message.
 * @param args          The args to replace in the message.
 * @returns {string}    The string built.
 */
Lang.prototype._replaceArgsInText = function (message, args, lang) {
        for (var i = 0; i < args.length; i++) {
            message = message.replace((this.PATTERN_ARGS + i), args[i]);
        }

        // Check if some of the args was other language keys.
        return message;
    };
/**
*用args替换文本中的标记。
*@param message将消息发送给您。
*@param args消息中要替换的args。
*@返回{string}生成的字符串。
*/
Lang.prototype.\u replaceArgsInText=函数(消息,args,Lang){
对于(变量i=0;i
编辑: 最终解决方案:

/**
 * Replace the tags in the text by the args.
 * @param message      The message.
 * @param args          The args to replace in the message.
 * @returns {string}    The string built.
 * @private
 */
private _replaceArgsInText(message: any, args: any, lang: string): string{
    for(var i = 0; i < args.length; i++){
        message = message.replace((this.PATTERN_ARGS+i), args[i]);
    }

    // Check if some of the args was other language keys.
    message = this._replaceSubKeys(message, /__\w+/g, this._languages[lang][this._FIELD_CONTENT_LANG]);

    return message;
}

/**
 * Replace the sub keys into the sentence by the actual text.
 * @param sentence
 * @param rx
 * @param array
 * @returns {string|void}
 * @private
 */
private _replaceSubKeys(sentence, rx, array): string{

    return sentence.replace(rx, function(i) {
        var subSentence = array[i];
        // TODO Check if that's an object or a string.
        if(typeof subSentence == 'string'){
            return subSentence;
        }else{
            console.log('not a string!')
            return null;
        }
    });
}
/**
*用args替换文本中的标记。
*@param message将消息发送给您。
*@param args消息中要替换的args。
*@返回{string}生成的字符串。
*@私人
*/
private\u replaceArgsInText(消息:any,args:any,lang:string):string{
对于(变量i=0;i
假设在映射中有替换参数,其名称为键,则可以执行以下操作:

function replace(text, rx, map) {
  return text.replace(rx, function(k) { return map[k] });
}

var res = replace(
  "The min length of __40 is 4. Max length is 16.",
  /__\w+/g, // regex for the substitution format
  {__40: 'snake'}); // map of substitution parameters
正则表达式是
/\uu w+/g
,以匹配参数的格式以及字符串中的所有参数。小提琴


我还使用了也带有函数的。

您要问的是什么语言?您的帖子被标记为
javascript
,但您的示例代码不是
javascript
。实际上是的,它是typescrit,所以它与javascript类似,但源代码是面向对象的。我刚刚更改了源代码,对此表示抱歉。现在是真的了。这里可能更好:谢谢你,这很有效@不客气!