Get everything but dashes at the beginning - regEx in javascript - javascript

I'm trying to build my regular expression, but I've failed.
I'm trying to get everything after single or double dash (you can try it here):
var regEx = /(?<=[-{1,2}])[^-]\S*/g;
It works just fine, but it selects even if we have 3+ dashes too. I've tried something like /(?<=^[-{1,2}])[^-]\S*/g and /(?<=\b[-{1,2}])[^-]\S*/g, but then it crashes at all.
Thanks in advance.

Unfortunately javascript doesn't support lookbehind
You can use this regex with multiline option
^-{1,2}(?!-)(\S*)
After this you can use group 1 to access the required match..

Related

Detect URL in text without hyperlink and replace normal URL with hyperlink?

I have spent considerable amount of time searching for the solution or trying one, But I did not found one. So my usecase is:
I have a text which can have simple url(with or without http/s) or it can also have hyperlinked url.
What regex should do
It should leave hyperlink url as it is and convert the non hyperlinked url to a hyperlinked URL.
Example Text
I am learning regex from www.codeburst.com and trying regex at Regexr.
Expected Solution
I am learning regex from www.codeburst.com and trying regex at Regexr.
I have tried
this regex, but it it not working as expected.
/((?!href).((https?:\/\/)||(www\.)|(mailto:)).+)/gi
You probably need a negative lookbehind (?<!href=") which was added to ECMAScript recently, see this answer
be careful with double || which renders tokend behind this useless (hungry match)
also be careful with .+ which matches everything after (including newline with /s regex option)
I would start with
(?<!href=")(((https?:\/\/)|(www\.)|(mailto:))\S+)
(https?:\/\/)?(www\.)?[-a-zA-Z0-9#:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9#:%_\+.~#?&//=]*)
The breakout of regex is as follows,
(https?:\/\/)? checks for http:\\ or https:\\ or no http
(www\.)?checks for www. or no www.
I have checked above regex with following test cases:
href="https://www.regexr.com"
href="http://www.regexr.com"
href="mailto:demo#demo.com"

Syntax error on regular expression in ExtendScript (Javascript ECMA-262 — Verison 3) [duplicate]

I have a regular expression testing for numbers(0-9) and/or forward slashes (/). It looks like this:
/^[0-9/]+$/i.test(value)
Now I believe this to be correct, but the eclipse javascript validator disagrees:
Syntax error on token "]", delete this token
I suppose this is because the separator/delimiter is / and eclipse 'thinks' the regex is finished (and therefore a ] would be unexpected).
We can satisfy eclipse by escaping the / like so:
/^[0-9\/]+$/i.test(value)
Note that both versions work for me.
My problem with this is:
As far as I know I do not need to escape the forward slash specifically in that range. It might be situation specific (as in, for javascript it is the used delimiter).
Although they both appear to be working, I'd rather use the 'correct' version because of behaviour in different environments, and, well.. because correct and all :)
Does anyone know what I'm supposed to do? Escape or not? I did not find any reputable site that told me to escape the / in a range, but the Eclipse-validator is probably not completely stupid...
The standard clearly says you can put anything unescaped in a character class except \, ] and newline:
RegularExpressionClassChar ::
RegularExpressionNonTerminator but not ] or \
RegularExpressionBackslashSequence
RegularExpressionNonTerminator ::
SourceCharacter but not LineTerminator
( http://es5.github.com/#x7.8.5 ). No need to escape /.
On the other side, I personally would escape everything when in doubt, just to make less smart parsers happy.

How to make meSpeak.js read special characters?

I would like to use mespeak.js script (based on speak.js based on espeak) for text-to-speech - which has a czech voice file -, but for some reason it skips czech special characters like ě, š, č, ř, ž and reads only the rest.
As espeak on Windows reads them correctly, I tried to compile a new voice file (cs.json), but the problem persists.
Thanks!
I don't know what those characters sound like, but your best bet might be to try to approximate the closest english sounding character combination.
For instance, if š sounds like sh in English (not saying it does) then just replace all instances of š with sh.
It may be a better way to use this e-speak modification instead of mespeak:
http://eeejay.github.io/espeak/emscripten/espeak.html (demo)

CKEDITOR.instance[x].setData not working in IE

Ok, I'm using the CKEditor in a web application. One thing I need to do it set the text in the text area. I've been using the line:
CKEDITOR.instances.setData(html);
...where html is a varible containing HTML.
This works fine in Chrome & Firefox, but not at all in Internet Explorer or Safari.
Can anyone provide an insight as to why, or suggest a work-around?
Many thanks in advance! :-)
Make sure to strip all newlines from the string you pass into setData(). An exception is thrown if you don't, with a message about an unterminated string. The newline characters used by CKEditor are the UNIX-style of \n (in other words, not the DOS version: \r\n).
The newline apparently throws off the parser, making it think that it's the end of the statement.
Also note that if you call getData() to get that value you just set again, CKEditor puts the line breaks and tabs back into it. You'll need to strip them out again if you need to set that value back using setData(). I use a regexp pattern like this to strip out the newlines (and tabs just for completeness):
[\n\t]+
Also make sure that if you use the regular expression to strip them, you need to make sure that the pattern matching will match the \n character (called "single-line" mode in .NET, but I don't know what you're using).

Validating browser upload file name and extension with simple regex

I got the regexp right. Works perfectly for Firefox ONLY. How would i make this cross browser, cross platform manner. Since it is file name and extension validation you are right i am using File Upload control.
^[a-zA-Z0-9_\.]{3,28}(.pdf|.txt|.doc|.docx|.png|.gif|.jpeg|.jpg|.zip|.rar)$
matches File name must not be empty[ 3, 28 characters long].
Extension must be within the group.
When this works superb in forefox i assume because the fileUpload.value = Filename.extension in firefox. It awfully fails in Google chrome and IE. I am using the above with .net Regular Expression validator and ClientScript enabled.
I know how to validate it on server, so please no server side solutions.
note:
Google chrome:
Provides the fileupload control value as c:\fakePath\filename.extension
IE:
Provides the Full path.
You can't use the ^ to start with if you sometimes have a full path but are only interested in the filename. The dot of the filending should be escaped.
You could try something like this:
[^\\/]{3,}\.(pdf|txt|doc|docx|png|gif|jpeg|jpg|zip|rar)$
As it looks you get only the file with Firefox but the full path with other browsers.
I'd always add a prefix / to your string and than validate the last part after the last fileseprator / or \.
This example uses lookahead to check the fileseparator (or manually added /) before the file and also allows the check of max 28 char for filename. see this online regex tester:
(?<=[\\/])[\w\.]{3,28}\.(?:pdf|txt|doc|docx|png|gif|jpeg|jpg|zip|rar)$
As things stand, your regex validates garbage like the following:
....pdf
____pdf
It also rejects perfectly valid files:
i.jpg
my-pic.jpg
pic.JPG
The easiest is to validate things in multiple steps:
Extract the extension:
\.[a-zA-Z]{3,4}$
Lowercase the extension and validate it against an array of acceptable values.
Optionally validate the file's name (though I'd recommend cleaning it instead):
[a-zA-Z0-9_-]+(?:\.[a-zA-Z0-9_-]+)*

Categories