How do I use .js to regex out the quotation marks in the following string?
var wtfx = "<div>ExternalClass=5"</div>44FB";
var wtf = /<div>ExternalClass.*>/;
wtf = wtfx.replace(wtf, "");
alert(wtf);
shows this does not work. If I take the '"' out then it does. How do I 'escape' the quote?
for example I'd like to use reg ex on the above wtf string to yield only the string 44FB.
not getting this.
The following Code will alert "44FB":
var wtfx = "<div>ExternalClass=5\"</div>44FB";
var wtf = /<div>ExternalClass.*>/;
wtf = wtfx.replace(wtf, "");
alert(wtf);
The only change is the Backslash before the Quotationmark in line 1.
However, I'm not 100% sure if that is what you want. Feel free to write a comment if you need another result.
Related
I am trying to divide a String in Swift. I have the following string
Program - /path/to/file.doc
I want to get three informations out of this string
Program
/path/to/file.doc
file.doc
I began with the following solution
var str = "Program - /path/to/file.doc"
let indi = str.rangeOfString("-")?.startIndex
let subString = str.substringWithRange(Range<String.Index>(start: str.startIndex, end: indi!))
let subString2 = str.substringWithRange(Range<String.Index>(start: indi!, end: str.endIndex))
This gives me the results
"Program "and
"- /path/to/file.doc"
But how can I get file.doc after the last /?
How Can i increase/decrease and range index to avoid blank spaces?
Yes, sidyll's suggestion is correct, it's a very common practice to get components of Unix path by converting it to NSURL. You may want to write something like this:
var str = "Program - /path/to/file.doc"
if let indi = str.rangeOfString(" - ")?.startIndex {
let subString = str.substringWithRange(Range<String.Index>(start: str.startIndex, end: indi))
let subString2 = str.substringWithRange(Range<String.Index>(start: indi, end: str.endIndex))
let fileName = NSURL(string: subString2).lastPathComponent()
}
I strongly suggest you don't do force unwrap like this. Consider situation if this code will work with string without a particular pattern, for example empty string. Correct, runtime error.
I want to write a regex in Ruby that will add a backslash prior to any open square brackets.
str = "my.name[0].hello.line[2]"
out = str.gsub(/\[/,"\\[")
# desired out = "my.name\[0].hello.line\[2]"
I've tried multiple combinations of backslashes in the substitution string and can't get it to leave a single backslash.
You don't need a regular expression here.
str = "my.name[0].hello.line[2]"
puts str.gsub('[', '\[')
# my.name\[0].hello.line\[2]
I tried your code and it worked correct:
str = "my.name[0].hello.line[2]"
out = str.gsub(/\[/,"\\[")
puts out #my.name\[0].hello.line\[2]
If you replace putswith p you get the inspect-version of the string:
p out #"my.name\\[0].hello.line\\[2]"
Please see the " and the masked \. Maybe you saw this result.
As Daniel already answered: You can also define the string with ' and don't need to mask the values.
I'm trying to reformat German dates (e.g. 13.03.2011 to 2011-03-13).
This is my code:
str = "13.03.2011\n14:30\n\nHannover Scorpions\n\nDEG Metro Stars\n60\n2 - 3\n\n\n\n13.03.2011\n14:30\n\nThomas Sabo Ice Tigers\n\nKrefeld Pinguine\n60\n2 - 3\n\n\n\n"
str = str.gsub("/(\d{2}).(\d{2}).(\d{4})/", "/$3-$2-$1/")
I get the same output like input. I also tried my code with and without leading and ending slashes, but I don't see a difference. Any hints?
I tried to store my regex'es in variables like find = /(\d{2}).(\d{2}).(\d{4})/ and replace = /$3-$2-$1/, so my code looked like this:
str = "13.03.2011\n14:30\n\nHannover Scorpions\n\nDEG Metro Stars\n60\n2 - 3\n\n\n\n13.03.2011\n14:30\n\nThomas Sabo Ice Tigers\n\nKrefeld Pinguine\n60\n2 - 3\n\n\n\n"
find = /(\d{2}).(\d{2}).(\d{4})/
replace = /$3-$2-$1/
str = str.gsub(find, replace)
TypeError: no implicit conversion of Regexp into String
from (irb):4:in `gsub'
Any suggestions for this problem?
First mistake is the regex delimiter. You do not need place the regex as string. Just place it inside a delimiter like //
Second mistake, you are using captured groups as $1. Replace those as \\1
str = str.gsub(/(\d{2})\.(\d{2})\.(\d{4})/, "\\3-\\2-\\1")
Also, notice I have escaped the . character with \., because in regex . means any character except \n
I'd like to know what to put in textpad's syntax file to fix the issue where, say, in an html file, you're writing a paragraph and an apostrophe creates syntax highlighting until the next aspostrophe.
Ex:
<p>Hi, I'm an example.
lol text here placeholder lorem ipsum I've died.</p>
I've placed in bold what would be color highlighted in textpad, for lack of stackoverflow coloring knowledge. :P It would be seen as similar to <a href='http://string.lol'> where you would normally use a pair of apostrophes or quotes. I realize that the issue may be in the way the syntax file is set up, where it's matching for any apostrophe instead of matching for an apostrophe not separated by a space. Ideally it would also need to match for equal signs and other common characters that would be seen directly next to an apostrophe or quote.
Here's where I believe it could be found inside the syntax file:
[Syntax]
Namespace1 = 6
IgnoreCase = No
InitKeyWordChars = A-Za-z_
KeyWordChars = A-Za-z0-9_
OperatorChars = -+*/!~%^&|=#`.,;:
KeyWordLength =
BracketChars = {[()]}
PreprocStart = #
HexPrefix = 0x
SyntaxStart =
SyntaxEnd =
CommentStart = /*
CommentEnd = */
CommentStartAlt = <!--
CommentEndAlt = -->
SingleComment = //
SingleCommentCol =
SingleCommentAlt =
SingleCommentColAlt =
SingleCommentEsc =
StringsSpanLines = Yes
StringStart = "
StringEnd = "
StringAlt = '
StringEsc = \
CharStart = '
CharEnd = '
CharEsc = \
You have your String options at the bottom, but is textpad capable of accepting some kind of expression matching or regex, and if so, how would I best do this? I've looked on google and here, and the keywords are just too vague to find anything that does exist on the topic, if anything does.
Thank you for any help you can provide.
I fixed this problem by editing the line in perl5.syn that reads
StringAlt = '
to instead be
; StringAlt = '
(the leading semi-colon comments out the StringAlt setting on that line; or you could just delete that line outright).
You need to use
SyntaxStart = <
SyntaxEnd = >
This will restrict syntax highlighting to only be within tags, and it's the best you can do with TextPad.
I've got a regular expression that I am using to strip an extension from another string
The extensions in this example are
BK|BZ|113
If the string does not contain any of the extensions, then I need it to leave the string as is.
The regular expression I'm using is
base_value = base_string[/(.*?[^-])-?(?:BK|BZ|113)/,1]
However, if the base_value does not contain the string, I want to return the base_value. I thought I could use
base_value = base_string
base_value = base_string[/(.*?[^-])-?(?:BK|BZ|113)/,1] unless (base_value !~ /(.*?[^-])-?(?:BK|BZ|113)) == false
but that isn't working.
What is the best way to return the base_string if the extensions are not found?
If I understand well, you want to strip the regexp from a string, if it matches, right? So.. do it:
string.sub(/(?:BK|BZ|113)$/, "")
Perhaps you can use sub like this:
base_string1 = "test1BK"
base_string2 = "test2"
p base_string1.sub(/(.*?[^-])-?(?:BK|BZ|113)/,'\1')
p base_string2.sub(/(.*?[^-])-?(?:BK|BZ|113)/,'\1')
When the pattern is not found nothing is replaced, otherwise it returns the string without the extension.
If /(.*?[^-])-?(?:BK|BZ|113)/ doesn't match then base_string[/(.*?[^-])-?(?:BK|BZ|113)/,1] will be nil. So this should work:
base_value = base_string[/(.*?[^-])-?(?:BK|BZ|113)/,1] || base_string