Invalid regular expression flags

This is my controller,

I am trying to get data from this controller by using code below but I am getting .

Can you please tell me what am I doing wrong?

4 Answers 4

@Url.Action only returns the action url’s string, without quotes around it.

You’ll need to wrap that url in quotes.

Otherwise, the file returned to the client will contain:

Which isn’t valid JS, nor what you want. The replacement gives the following result:

Which is a perfectly valid JavaScript string.

Please remove the Controller suffix while specifying in url .

Try it this way:

A Javascript regular expression literal looks like this — a pattern enclosed between slashes:

If you interpolate a variable that begins with a slash but do not put quotation marks around it, it will be interpreted as a regular expression, not a string. Another time this comes up is with JSP expression language, where you should write the first, not the second:

I am getting the error

Invalid regular expression flags

From the edit button when I click on it. But HTML output seems to be fine

Out come of generated button from JavaScript

2 Answers 2

The error is in the code onclick=’location.href=" + directlink + "’ . Here, location.href is assigned a RegEx(Note that regex literal syntax uses / as delimiters).

Here, /Products/edit/11 is read as Regex /Products/ and edit as regex flags, which are Invalid flags(except i ).

To solve this, the value of the URL can simply be wrapped inside quotes. As both single and double quotes are already used, the quotes need to be escaped.

Читайте также:  Hyundai creta advanced style

If target environment support EcmaScript 6 template literals, you can use

As, yo’re using jQuery, I’d suggest to use it to create new element and bind events on it.

Thanks to modern browsers, writing cross-browser compatible JavaScript isn’t nearly as difficult as it was even just a few years ago; however, there are still times when you get cryptic error messages such as “invalid regular expression flag.”

Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.

It’s funny because it’s true, right? But we still have regular expressions, they’re still powerful, and we still use them.

Unfortunately, there are still times in which they can plague us when working with JavaScript, string replacement, and modern web browsers.

The Use Case

Earlier this week, I was working on a project in which a user would be making a selection from a `select` box.

I had an event handler attached to `select` element’s `onchange` event so that when the user made their selection, the DOM would be manipulated in such a way that certain elements would have their spaces replaced with a hyphen.

Specifically, the original code looked something like this:

Now, up until a certain point in testing I had no problem with this. In fact, it was working fine in WebKit based browsers, but it tanked in Firefox.

Invalid Regular Expression Flag

Of course, this is really a manifestation of laziness or forgetfulness (I’d much rather assume the latter) on my part because the method signature for `replace` is `regex/substr, newString`.

Although certain implementations of JavaScript will support a substring (like what I had originally), a regular expression is the more widely accept method.

Читайте также:  Как зарегистрировать адрес электронной почты

As such, it required a change to the code as follows:

And thus, the problem was resolved.

So What’s The Deal?

As I mentioned, it really depends on the browsers implementation of the JavaScript interpreter, but here’s a more literal way of looking at it:

  • The first version of the code was saying “replace all substring spaces with a hyphen.”
  • The second version has a regular expression that says “replace any occurrence of a space that occurs globally in the string.”

There’s obviously very little difference in the two implementations, but – again – it all comes down to the implementation of JavaScript.

If regular expressions are the argument accepted in a function signature, then that’s the one you’ve gotta use. Unless, of course, you’re only building for WebKit based browsers, but that’s an entirely other post.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

Adblock detector