首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript RegExp和String.replace()方法的质疑

问JavaScript RegExp和String.replace()方法的质疑
EN

Stack Overflow用户
提问于 2017-08-30 06:39:11
回答 2查看 71关注 0票数 0

我试图输入‘用户名’在一个网页使用VBA。因此,在网页的源代码中,对“用户名”值做了一些修改。

我附了密码,

代码语言:javascript
复制
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的新手,所以如果我太慢,请原谅我。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-08-30 07:49:33

看起来你正在使用一些非常古老的代码来学习。☹让我们看看我们是否还能通过更新这些代码来学习一些东西,然后你会找到一些更新的学习材料。这是一本写得很好的书系列,提供免费的在线版本:你不了解JS。

代码语言:javascript
复制
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
}

二次尝试

代码语言:javascript
复制
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 );
}

第三,让我们把这件事清理一下:

代码语言:javascript
复制
// 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的所有修改链接在一起,我们可以使它变得更短、更容易阅读。让我们也给我们的函数一个有用的名称,并尝试它对一个坏字符串。

代码语言:javascript
复制
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()
}
票数 0
EN

Stack Overflow用户

发布于 2017-08-30 06:51:09

让我们一个一个地看一遍这些陈述

代码语言:javascript
复制
document.str.value  = document.str.value.toUpperCase();

使字符串大写

代码语言:javascript
复制
pattern = new RegExp("\\*", "g");
document.str.value = document.str.value.replace(pattern, "");

替换0和无限出现的\字符之间,因此在这种情况下没有匹配。

代码语言:javascript
复制
document.str.value = document.str.value.replace(/^\s+/, "");

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

代码语言:javascript
复制
document.str.value = document.str.value.replace(/\s+$/, "");

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

你是正确的。以"Abc02023"作为输入,输出就是您所建议的。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45953612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档