Have a look at this fiddle http://jsfiddle.net/yeXWv/
Here I want to split the characters which starts with [* or [*# and ends with *]. The current regular expression split the string which starts with [*# but not [*. I have tried the following patterns,
/(\[\*\#*[a-zA-Z0-9]+\*\])/g
/(\[\*\#{0,1}[a-zA-Z0-9]+\*\])/g
Thanks in advance.
Try making the hash character optional:
/(\[\*\#?[a-zA-Z0-9 ]+\*\])/g
Edit: added missing white space :-)
You forgot to allow for spaces, which was the real problem -- not the missing # character.
/(\[\*\#?[a-zA-Z0-9 ]+\*\])/g
That will preserve the [*...*] strings in the output array. To omit them, remove the parentheses:
/\[\*\#?[a-zA-Z0-9 ]+\*\]/g
http://jsfiddle.net/mblase75/zU576/
Related
I have this reg-ex to validate comma separated values:
regex = "/^[-\w\s]+(?:,[-\w\s]+)*$/"
Currently there are no special characters allowed.
What modification to this can be made to allow special characters in each comma separated value?
Just add wanted special characters inside the character class like, for example:
/^[-\w\s#|#%]+(?:,[-\w\s#|#%]+)*$/
// ^^^^ ^^^^
You can add any character you want.
#Harman,
I cannot suggest edits in your regex, but I have one regex which I used sometime back in my code to incorporate special characters too.
Try this one:
(?:^|,\s{0,})(["]?)\s{0,}((?:.|\n|\r)*?)\1(?=[,]\s{0,}|$)
You can try this regex here
Hope this will be helpful for you!
I want to get the last string between special characters. I've done for square bracket as \[(.*)\]$
But, when I use it on something like Blah [Hi]How is this[KoTuWa]. I get the result as [Hi]How is this[KoTuWa].
How do i modify it to get the last stringthat is KotuWa.
Also, I would like to generalise to general special characters, instead of just matching the string between square brackets as above.
Thanks,
Sai
I would do this:
[^[\]]+(?=][^[\]]*$)
Debuggex Demo
To extend this to other types of brackets/special chars, say I also wanna match curly braces { and double quotes ":
[^{}"[\]]+(?=["\]}][^{}"[\]]*$)
Debuggex Demo (I added the multi-line /m only to show multiple examples)
Here is one way to do it:
\[([^\[]*)\]$
You can require that the string between brackets does not contain brackets:
Edit: thanks to funkwurm and jcubic for pointing out an error. Here's the fixed expression:
\[([^[]+)\][^\[]*$
If you need to use other separators than brackets, you should:
replace the \[ and \] with your new separators
replace the negative character classes with your beginning separator.
For example, assuming you need to use the separators <> instead of [], you'd do this:
<([^<]+)>[^\>]*$
How to remove text and whitespace?
For example:
http://www.exampleweb.com/myprogram.rar
http://www.examplebackup.com/mybackups.rar
http://www.exampleweb.com/myprogram1.rar
I use something likes this to get rid of second line:
http://www.examplebackup.com/.+?.rar
End result is:
http://www.exampleweb.com/myprogram.rar
http://www.exampleweb.com/myprogram1.rar
But I want something like that with no whitespaces left:
http://www.exampleweb.com/myprogram.rar
http://www.exampleweb.com/myprogram1.rar
Please help I'm total noob in regex.
Well, you regex doesn't match the newline at the end, so it won't affect it. To fix this, add \n to the end.
Regex101 demo of the idea -- notice, on the right, how the match isn't movie, it's movie with a newline after it.
A few other things that may or may not apply to your actual regex that I'd like to recommend:
You need to escape your slashes (/ => \/) in your regex, or it might cause issues.
You need to escape literal full stops (. => \.) or it'll match any character in that position (i.e. backup.rar will match backup:rar as well)
I think you may be forgetting to escape your "any char" operator. Try this:
http://www.examplebackup.com/mybackups/.+?\.rar
Tried to search for /\,$/ online, but coudnt find anything.
I have:
coords = coords.replace(/\,$/, "");
Im guessing it returns coords string index number. What I have to search online for this, so I can learn more?
/\,$/ finds the comma character (,) at the end of a string (denoted by the $) and replaces it with empty (""). You sometimes see this in regex code aiming to clean up excerpts of text.
It's a regular expression to remove a trailing comma.
That thing is a Regular Expression, also known as regex or regexp. It is a way to "match" strings using some rules. If you want to learn how to use it in JavaScript, read the Mozilla Developer Network page about RegExp.
By the way, regular expressions are also available on most languages and in some tools. It is a very useful thing to learn.
That's a regular expression that finds a comma at the end of a string. That code removes the comma.
// defines a JavaScript regular expression, used to match a pattern within a string.
\,$ is the pattern
In this case \, translates to ,. A backslash is used to escape special characters, but in this case, it's not necessary. An example where it would be necessary would be to remove trailing periods. If you tried to do that with /.$/ the period here has a different meaning; it is used as a wildcard to match [almost] any character (aside for some newlines). So in this case to match on "." (period character) you would have to escape the wildcard (/\.$/).
When $ is placed at the end of the pattern, it means only look at the end of the string. This means that you can't mistakingly find a comma anywhere in the middle of the string (e.g., not after help in help, me,), only at the end (trailing). It also speeds of the regular expression search considerably. If you wanted to match on characters only at the beginning of the string, you would start off the pattern with a carat (^), for instance /^,/ would find a comma at the start of a string if one existed.
It's also important to note that you're only removing one comma, whereas if you use the plus (+) after the comma, you'd be replacing one or more: /,+$/.
Without the +; trailing commas,, becomes trailing commas,
With the +; no trailing comma,, becomes no trailing comma
I have everything in place to create slugs from titles, but there is one issue. My RegEx replaces spaces with hyphens. But when a user types "Hi there" (multiple spaces) the slug ends up as "Hi-----there". When really it should be "Hi-there".
Should I create the regular expression so that it only replaces a space when there is a character either side?
Or is there an easier way to do this?
I use this:
yourslug.replace(/\W+/g, '-')
This replaces all occurrences of one or more non-alphanumeric characters with a single dash.
Just match multiple whitespace characters.
s/\s+/-/g
Daniel's answer is correct.
However if somebody is looking for complete solution I like this function,
http://dense13.com/blog/2009/05/03/converting-string-to-slug-javascript/
Thanks to "dense13"!
It might be the easiest to fold repeated -s into one - as the last step:
replace /-{2,}/ by "-"
Or if you only want this to affect spaces, fold spaces instead (before the other steps, obviously)
I would replace [\s]+ with '-' and then replace [^\w-] with ''
You may want to trim the string first, to avoid leading and trailing hyphens.
function hyphenSpace(s){
s= (s.trim)? s.trim(): s.replace(/^\s+|\s+$/g,'');
return s.split(/\s+/).join('-');
}