Series 2 offer to prove safety

Title Description

Please implement a function, a string to replace each space to "20%." For example, when the string through the string We Are Happy replaced after We% 20Are% 20Happy.;

Thinking

This question is examined string replacement operation, so we used the method of string replacement method replace.

replace () method for replacing some characters other characters, or alternatively a substring match the positive expression in using string.

replace syntax

stringObject.replace(regexp/substr,replacement)

parameter description
regexp/substr

essential. RegExp object to replace or substring predetermined pattern.

Note, if the value is a string, then it is as a direct amount to be retrieved text mode, without first being converted into RegExp object.

replacement essential. A string value. Alternatively the predetermined function generates replacement text or text.

Returns a new string with  replacement  replaced regexp obtained after the first of all a match or matches.

achieve

function replaceSpace(str)
{
    // write code here
    return str.replace(/ /g, '%20')
}
module.exports = {
    replaceSpace : replaceSpace
};

 

Guess you like

Origin www.cnblogs.com/xiaochensun/p/11583099.html