Escape "+" in JavaScript Regular Expression [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
This is the code I am trying to execute
input.replace(/^.+?(?=\+)/, ''), "i")
I have escape plus with \+ but I get error
Uncaught SyntaxError: Invalid regular expression: /+/: Nothing to repeat
Kindly let me know how to escape + in the above regexp.

you need to put the i modifier at the end of the regex - not as a separate parameter. For example:
input.replace(/^.+?(?=\+)/i, '');
As #LorenzMeyer has pointed out, you don't actually need the i modifier, because the case is irrelevant based on your regex. Perhaps you need a global replace? In which case your replace would look like this:
input.replace(/^.+?(?=\+)/g, '');

Related

Replace the <img /> tag from a string using javascript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
How could I replace or remove all the image tag in my string using javascript?
Coffee Bean<div><br /></div><img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/4gxYSUNDX.." />
I want to remove the image and the successive image tag in a string. How to do it?
Using Regex:
myString.replace(/<img[^>]*>/g,"");
[^>]* means any number of characters other than >. If you use .+ instead, if there are multiple tags the replace operation removes them all at once, including any content between them. Operations are greedy by default, meaning they use the largest possible valid match.
/g at the end means replace all occurrences (by default, it only removes the first occurrence).

Regex for background image [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I've made a regular expression for getting all background image patterns:
Pattern p = Pattern.compile("background(-image)?:[\\s]?url[\\s]*\([\\s]*(?<url>[^\)]*)[\\s]*\)[\\‌​s]*");
But this will failed in this case, because of #66cc33:
background:#66CC33 url(images/bg-topbar.png)
Can anyone help me to modify my pattern?
You can use this regex, with basically doesn't care about anything but the url() content:
background(-image)?:.*?url\(\s*(?<url>.*?)\s*\)
This seems like a duplicate of this question https://stackoverflow.com/a/20857448/5856415, you should try the regex given in that answer to simply select text between the brackets.
/\((.*?)\)/)[1].replace(/('|")/g

Regex to avoid spaces AND a dot followed by hyphen or hyphen followed by a dot [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need a regex for an input pattern that doesn't allow spaces and a "." followed by a "-"
i.e.:
this-is-valid.com
this is not valid
this.-is-also-not-valid
this-.is-also-not-valid
The key will be defining the group of characters that are allowed on the left and right sides and just adding "-" to the right-hand-side. I've just used \w here:
\b((?:\w|-)+\.\w+)(?:[^-]|$)
[Live Example]

javascript regexp pattern definition [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I need help
I have a form feild I need to add validation to. The format of the value has to be specific. I want to define a regex pattern to handle it.
The format needs to allow L123456
L = it has to be L which is the start of out skus
123456 = I need to confirm they are entering a 6 digit number.
thanks
Jeff
var str = "L123456";
/^L\d{6}$/.test(str)
The pattern uses the ^ to determine the strings starts with. \d{6} states 6 digits, and $ means end of string. See: http://www.regular-expressions.info/ for more info.

Regex Clarification [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am relatively new to regex. I found this regex being used and was not able to understand how exactly it is working.
([\(]*([\w][\/]?[\w]?(%)?(,)?)*|[\/.]|[\w]([\-\*|\+|\\|\<\>|\=]([\(]*[\w][\/]?(%)?[\)]?[\)]?(,)?)+)*[\)]?)*$/
I understand most of the basic syntax but not able to understand what this means.
Any clarity on this will be appreciated! This is used in javascript so the tag.
Thanks!
The same without uneeded characters:
/(\(*(\w\/?\w?(%)?(,)?)*|[\/.]|\w([-*+\\<>=](\(*\w\/?(%)?\)?\)?(,)?)+)*\)?)*$/
This pattern can match this kind of string (or nothing):
(((a/b%,(((a/b%,///./././(((a/b%,k*((((((((P/%)),)
your regex:
([\(]*([\w][\/]?[\w]?(%)?(,)?)*|[\/.]|[\w]([\-\*|\+|\\|\<\>|\=]([\(]*[\w][\/]?(%)?[\)]?[\)]?(,)?)+)*[\)]?)*$/
can be visualized as:
Debuggex Demo
unless there is a typo in your regex, it will never match anything; the regex ends with $/ which means end of the string followed by /, unless you are matching over multiple lines. If this was homework, I would say the teacher is making a bad joke because of the $/ that doesn't usually match anything.
After some simplification you get:
(\(*(\w\/?\w?(%)?(,)?)*|[\/.]|\w([-*|+\\<>=](\(*\w\/?(%)?\)?\)?(,)?)+)*\)?)*$/
If you don't care about grouping, then this is similar:
(\(|[\/.]|(\w\/?\w?%?,?)|\w([-*|+\\<>=](\(*\w\/?%?\)?\)?,?)+)*\)?)*$/
Debuggex Demo
this takes advantage that (a*|b*)* can be simplified into (a|b)*
To learn more about regex REFER : regex101 give your code there and read explanation .
Your regex: ([\(]*([\w][\/]?[\w]?(%)?(,)?)*|[\/.]|[\w]([\-\*|\+|\\|\<\>|\=]([\(]*[\w][\/]?(%)?[\)]?[\)]?(,)?)+)*[\)]?)*$
Will match anything like
(d/f%,
something
122
But will fail to match something like
--{}
Explanation :

Categories