怀疑JavaScript RegExp和String.replace()方法

时间:2017-08-30 06:39:12

标签: javascript

我正在尝试使用VBA在网页中输入“用户名”。因此,在网页的源代码中,对“用户名”值进行了一些修改。

我附上了代码,

function myFunction()        
   {
    document.str.value = "Abc02023";
    document.str.value  = document.str.value.toUpperCase();
    pattern = new RegExp("\\*", "g");
    document.str.value = document.str.value.replace(pattern, "");
    document.str.value = document.str.value.replace(/^\s+/, "");
    document.str.value = document.str.value.replace(/\s+$/, "");
   }

我阅读了这些内容,根据我的理解,修改后的document.str.value为 ABC02023 。 显然我错了,因为那时做所有这些修改毫无意义。此外,我收到“错误的用户名错误”。

任何人都可以帮助我理解这些。 document.str.value的价值是什么?你是如何理解的?我是JavaScript的新手,所以如果我太慢,请原谅我......

3 个答案:

答案 0 :(得分:0)

看起来您正在使用一些非常古老的代码来学习。 ☹ 让我们看看我们是否仍然可以通过更新这些代码来学习一些东西,然后你会找到一些更新的学习资料。这是一本精心编写的书籍系列,提供免费在线版本:You Don't Know JS

function myFunction() {

    // Assuming your code runs in a browser, `document` is equal to the
    // global object. So if in a browser and somewhere outside the function
    // a variable `str` has been created, this will add an attribute `value`
    // to `str` and set the value of `str.value` to 'Abc02023'. If there is 
    // no already existing object `document` (for instance not running in 
    // a browser) or if document does not have an already created property
    // called`str` then this will throw a TypeError because you cannot add 
    // a property to `undefined`.

    document.str.value = "Abc02023";

    // You probably were just trying to create a new variable `str` so let's
    // just start over
}

第二次尝试

function myFunction() {
    // create a variable `str` and set it to 'Abc02023'

    var str = "Abc02023";

    // Take value of str and convert it to all capital letters
    //   then overwrite current value of str with the result.
    //   So now `str === 'ABC02023'

    str  = str.toUpperCase();

    // Create a regular expression representing all occurences of `*`
    //   and assign it to variable `pattern`.

    var pattern = new RegExp("\\*", "g");

    // Remove all instances of our pattern from the string. (which does not
    // affect this string, but prevents user inputting some types of bad
    // strings to hack your website.

    str = str.replace(pattern, "");

    // Remove any leading whitespace form our string (which does not
    //  affect this string, but cleans up strings input by a user).

    str = str.replace(/^\s+/, "");

    // Remove any trailing whitespace form our string (which does not
    //  affect this string, but cleans up strings input by a user).

    str = str.replace(/\s+$/, "");

    // Let's at least see our result behind the scenes. Press F12 
    //  to see the developer console in most browsers.

    console.log("`str` is equal to: ", str );
}

第三次尝试,让我们清理一下:

// The reason to use functions is so we can contain the logic 
//  separate from the data. Let's pull extract our data (the string value)
//  and then pass it in as a function parameter

var result = myFunction('Abc02023')
console.log('result = ', result)

function myFunction(str) {

    str  = str.toUpperCase();

   // Nicer syntax for defining regular expression.

    var pattern = /\*/g;

    str = str.replace(pattern, '');

    //  Unnecesarry use of regular expressions. Let's use trim instead
    //   to clean leading and trailing whitespace at once.

    str = str.trim()

    // let's return our result so the rest of the program can use it

    // return str
}

最后一轮。通过将对str的所有修改链接在一起,我们可以使这更短,更容易阅读。而且我们还给我们的函数一个有用的名称并尝试对付错误的字符串。

var cleanString1 = toCleanedCaps('Abc02023')
var cleanString2 = toCleanedCaps(' ** test * ')

console.log('cleanString1 = ', cleanString1)
console.log('cleanString2 = ', cleanString2)

function toCleanedCaps(str) {
    return str
        .toUpperCase()
        .replace(/\\*/g, '')
        .trim()
}

答案 1 :(得分:0)

@skylize答案很接近

实际上等同于您的代码

function toCleanedCaps(str) {
    return str
        .toUpperCase()
        .replace(/\*/g, '') // he got this wrong
        .trim()
}

答案 2 :(得分:-1)

让我们逐一回顾一下这些陈述

document.str.value  = document.str.value.toUpperCase();

使字符串大写

pattern = new RegExp("\\*", "g");
document.str.value = document.str.value.replace(pattern, "");

替换\字符的零和无限次出现,因此在这种情况下不匹配。

document.str.value = document.str.value.replace(/^\s+/, "");

替换字符串开头一次和无限次出现的任何空格字符,因此不匹配。

document.str.value = document.str.value.replace(/\s+$/, "");

替换字符串末尾一次和无限次出现的任何空白字符,因此不匹配。

你是对的。以"Abc02023"为输入,输出就是您的建议。