javascript 正则表达式- val.replace(/^[^a-zA-Z0-9]*|[^a-zA-Z0-9]*$/g,"'');

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26857934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:36:04  来源:igfitidea点击:

Regular expression - val.replace(/^[^a-zA-Z0-9]*|[^a-zA-Z0-9]*$/g,"'');

javascriptjqueryregexweb

提问by SsNewbie

I was learning regular expression, It seems very much confusing to me for now.

我正在学习正则表达式,现在对我来说似乎很困惑。

val.replace(/^[^a-zA-Z0-9]*|[^a-zA-Z0-9]*$/g, '');

In the above expression

在上面的表达式中

1) which part denotes not to include white space? as i am trying to exclude all non alphanumeric characters.

1) 哪个部分表示不包含空格?因为我试图排除所有非字母数字字符。

2) Since i don't want to use even '$' and ''(underscore) can i specify '$' & ''(underscore) in expression something like below?

2) 由于我什至不想使用 '$' 和 ' '(下划线),我可以在如下所示的表达式中指定 '$' 和 ''(下划线)吗?

val.replace(/^[^a-zA-Z0-9$_]*|[^a-zA-Z0-9$_]*/g, '');?

3) As 'x|y' specify that - "Find any of the alternatives specified". Then Why we have used something like this [^a-zA-Z0-9]|[^a-zA-Z0-9]which is same on both sides?

3) 如 ' x|y' 指定 - “查找任何指定的替代方案”。那么为什么我们使用了这样的东西[^a-zA-Z0-9]|[^a-zA-Z0-9],两边都一样?

Please help me understand this, Finding it bit confused and difficult.

请帮助我理解这一点,发现它有点困惑和困难。

回答by

Some basic info.

一些基本信息。

When you read regular expressions, you read them from left to right. That's how the engine does it.

当您阅读正则表达式时,您是从左到右阅读它们的。发动机就是这样工作的。

This is important in the case of alternations as the one on the left side(s) are always tried first.

这在交替的情况下很重要,因为总是首先尝试左侧的那个。

But in the case of a $(EOL or EOS) anchor, it might be easier to read from right to left.

但在$(EOL 或 EOS)锚点的情况下,从右到左阅读可能更容易。

Built-in assertions like line break anchors ^$and word boundry \balong with normal assertions look ahead (?=)(?!)and look behind (?<=)(?<!), do not consume characters.

内置断言(如换行锚点^$和单词边界)\b以及正常断言向前看(?=)(?!)和向后看(?<=)(?<!),不消耗字符。

They are like single path in-line conditionals that pass or fail, where only if it passes will the expression to the right of it be examined. So they do actually Matchsomething, they match a condition.

它们就像通过或失败的单路径内嵌条件,只有当它通过时才会检查它右侧的表达式。所以他们确实匹配一些东西,他们匹配一个条件。

Format your regex so you can see what its doing. (Use a app to help you RegexFormat 5)

格式化您的正则表达式,以便您可以看到它在做什么。(使用应用程序帮助您RegexFormat 5

   ^                # BOS
   [^a-zA-Z0-9]*    # Optional not any alphanum chars
|                 # or, 
   [^a-zA-Z0-9]*    # Optional not any alphanum chars
   $                # EOS

Your regex in global context will always match twice, once at the beginning of the string, once at the end because of the line break anchors and because you don't actually require anything else to match.

您在全局上下文中的正则表达式将始终匹配两次,一次在字符串的开头,一次在结尾,因为换行符锚点以及您实际上不需要任何其他内容来匹配。

So basically you should avoid trying to match (mix) all optional things with the built-in anchors ^$\b. That means your regex is better represented by ^[^a-zA-Z0-9]+|[^a-zA-Z0-9]+$since you don't care if its NOTthere (in the case of *, zeroor more quantifier).

所以基本上你应该避免尝试用内置的 anchors 匹配(混合)所有可选的东西^$\b。这意味着你的正则表达式是更好地为代表^[^a-zA-Z0-9]+|[^a-zA-Z0-9]+$,因为你不关心,如果它存在(在的情况下*或更多量词)。

Good Luck, keep studying.

祝你好运,继续学习。

回答by Jithin

This regular expression replaces all starting and trailing non alphanumeric characters from the string.

此正则表达式替换字符串中的所有开头和结尾的非字母数字字符。

  1. It doesn't specifically specifies whitespace. It just negates every thing other than alphanumeric characters. Whatever inside square bracket is a character set - [Whatever]. A starting cap(^) INSIDE the character set says its a negation. So [^a-zA-Z0-9]*says zero or more characters which are other than a-z, A-z or 0-9.

  2. The $sign at the end says, to the end of string and nothing to do with $ and _ symbols. That will be already included in the character set as it all non alpha numeric characters.

  3. Refer answer of @smathy.

  1. 它没有特别指定空格。它只是否定除字母数字字符之外的所有内容。方括号内的任何内容都是字符集 - [Whatever]^字符集内的起始 cap( ) 表示其否定。所以[^a-zA-Z0-9]*说零个或多个除 az、Az 或 0-9 之外的字符。

  2. 最后的$符号表示,到字符串的末尾,与 $ 和 _ 符号无关。这将已经包含在字符集中,因为它是所有非字母数字字符。

  3. 参考@smathy 的回答。

Also just FYI, AFAIU regular expression can't be learned by scrolling a tutorial. You just need to go through the basics and try out the examples.

同样仅供参考,无法通过滚动教程来学习 AFAIU 正则表达式。您只需要了解基础知识并尝试示例。

回答by smathy

To answer your third question, the alternatives run all the way to the //s, so both sides are not the same. In the original regex the left alternative is "all non alphanumerics at the startof the string" and the right alternative is "all non alphanumerics at the endof the string".

回答你的第三个问题,备选方案一直跑到//s,所以两边都不一样。在原始的正则表达式中,左边的选择是“字符串开头的所有非字母数字”,右边的选择是“字符串末尾的所有非字母数字”。