admin管理员组文章数量:1134247
I don't have much experience with JavaScript but i'm trying to create a tag system which, instead of using @
or #
, would use /
.
var start = /#/ig; // @ Match
var word = /#(\w+)/ig; //@abc Match
How could I use a /
instead of the #
. I've tried doing var slash = '/'
and adding + slash +
, but that failed.
I don't have much experience with JavaScript but i'm trying to create a tag system which, instead of using @
or #
, would use /
.
var start = /#/ig; // @ Match
var word = /#(\w+)/ig; //@abc Match
How could I use a /
instead of the #
. I've tried doing var slash = '/'
and adding + slash +
, but that failed.
- 2 meta.stackexchange.com/a/177767/178816 – Travis J Commented May 20, 2013 at 19:53
9 Answers
Reset to default 168You can escape it like this.
/\//ig; // Matches /
or just use indexOf
if(str.indexOf("/") > -1)
You need to escape the /
with a \
.
/\//ig // matches /
You can escape it by preceding it with a \
(making it \/
), or you could use new RegExp('/')
to avoid escaping the regex.
See example in JSFiddle.
'/'.match(/\//) // matches /
'/'.match(new RegExp('/') // matches /
If you want to use /
you need to escape it with a \
var word = /\/(\w+)/ig;
In regular expressions, "/" is a special character which needs to be escaped (AKA flagged by placing a \ before it thus negating any specialized function it might serve).
Here's what you need:
var word = /\/(\w+)/ig; // /abc Match
Read up on RegEx special characters here: http://www.regular-expressions.info/characters.html
You can also work around special JS handling of the forward slash by enclosing it in a character group, like so:
const start = /[/]/g;
"/dev/null".match(start) // => ["/", "/"]
const word = /[/](\w+)/ig;
"/dev/null".match(word) // => ["/dev", "/null"]
For me, I was trying to match on the /
in a date in C#. I did it just by using (\/)
:
string pattern = "([0-9])([0-9])?(\/)([0-9])([0-9])?(\/)(\d{4})";
string text = "Start Date: 4/1/2018";
Match m = Regex.Match(text, pattern);
if (m.Success)
{
Console.WriteLine(match.Groups[0].Value); // 4/1/2018
}
else
{
Console.WriteLine("Not Found!");
}
JavaScript should also be able to similarly use (\/)
.
I encountered two issues related to the foregoing, when extracting text delimited by \
and /
, and found a solution that fits both, other than using new RegExp
, which requires \\\\
at the start. These findings are in Chrome and IE11.
The regular expression
/\\(.*)\//g
does not work. I think the //
is interpreted as the start of a comment in spite of the escape character. The regular expression (equally valid in my case though not in general)
/\b/\\(.*)\/\b/g
does not work either. I think the second /
terminates the regular expression in spite of the escape character.
What does work for me is to represent /
as \x2F
, which is the hexadecimal representation of /
. I think that's more efficient and understandable than using new RegExp
, but of course it needs a comment to identify the hex code.
Forward Slash is special character so,you have to add a backslash before forward slash to make it work
$patterm = "/[0-9]{2}+(?:-|.|\/)+[a-zA-Z]{3}+(?:-|.|\/)+[0-9]{4}/";
where / represents search for / In this way you
本文标签: javascriptMatching a Forward Slash with a regexStack Overflow
版权声明:本文标题:javascript - Matching a Forward Slash with a regex - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736788792a1952990.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论