How to replace Javascript multiline comments with text in middle using regex - javascript

I am trying to replace a two multiline comments (on a single line) with javascript text in the middle. I am using a build tool, which reads the entire file, and need to replace a specific string (made up of comments) during the build.
Example:
var data = /*testThisDelete:start*/new Date();/*testThisDelete:end*/
Once replaced, should used like this
var data = 4.6.88

Try something like this to get started:
"your file as a string".replace(new RegExp('/\*testThisDelete\:start.*testThisDelete\:end\*/','m'), '"replacement text"');
See this post for a lot of useful additional info: JavaScript replace/regex

Are you looking for:
^.+?(\/\*testThisDelete:start\*\/.+?\/\*testThisDelete:end\*\/)$
With this you should just be able to replace the first matched substring with what you want.

Related

Recursive regex pattern in JavaScript

I know it's going to be a VERY obvious answer, but I can't find anything on how to do this.
I'm trying to unescape < and > within an HTML string
My test output string is essentially:
```php
>h2<Heading2>/h2<
```
`>h2<Heading2>/h2<`
>h2<Heading2>/h2<
So in this example we have Github flavoured Markdown, a regular code markdown snippet, and then raw text all with the same HTML tag. I want to unescape the raw tag (the third one) to actually become a link. The ideal output would be something like this.
```php
>h2<Heading2>/h2<
```
`>h2<Heading2>/h2<`
<h2>Heading2</h2>
I'm getting stuck at getting multiple > in the same line.
Current regex:
/(?:.*?(>))/
This will get the first entry.
/(?:.*?(>))/g
This one gets the second entry. I want it to be able to get EVERY entry. Then, it's just a matter of throwing the tick pieces.
/(?:```|`)(?:.*?(>)).*?(?:```|`)/gs
If you're intending on using a regular expression for this task, you can consider the following:
var r = s.replace(/((`(?:``)?)[^`]*\2)|>/g, '$1<')
.replace(/((`(?:``)?)[^`]*\2)|</g, '$1>')
.replace(/`[<>]+/g, '`');
Working Demo

Text Replacement With RegEx

I am using Sublime Text to write some Javascript and need to do a simple text replacement in the editor in order to set code up. I can do it manually but I figured there must be a way to have the replacement occur automatically with RegEx. I've used RegEx a bunch before but have never used it to grab data from one part of the code to reference and edit another part of the code. For example, I have this:
var example_1 = 836;
var example_2 = 837;
var example_3 = 838;
var example_4 = 846;
And then I have this:
SELECT_122=836
SELECT_143=837
SELECT_144=838
SELECT_145=846
I want these to use the corresponding values and format them like this:
SELECT_122: example_1,
SELECT_143: example_2,
SELECT_144: example_3,
SELECT_145: example_4
Note that I'm updating the equal signs to colons with spaces so I figured doing all these changes could be done with some sort of search and replace. I have a large amount of these so I figured it would be best to learn how to do this if it's possible.
I don't have SublimeText, but you said in a comment that you want to do it through a text editor. Here is what works for me in EditPad Pro, it may work in Sublime.
Search:
(?s)(var (example_\d++) = (\d++).*?SELECT_\d++)=\3
Replace:
\1: \2,
Then I click "Replace". This will replace the first instance (SELECT_122=836) with "SELECT_122: example_1,"
Then I click "Replace Next" multiple times, and the SELECT_ strings are left looking like this:
SELECT_122: example_1,
SELECT_143: example_2,
SELECT_144: example_3,
SELECT_145: example_4,
Is this what you want?
Hope the regex and replacement string at least get you started. :)

JavaScript importing and scanning text file

I am trying to convert a Java program I wrote into JavaScript.
One of the functionalities of the program include importing a text file that has about a thousand words, separated with line breaks between every word.
In Java, importing this text file and putting it in an array variable was easy, since I could use Scanner.nextLine() and for loops. But I have no idea how I am supposed to do this in JavaScript.
Is there an object analogous to Java's scanner in JavaScript? How can I achieve this task?
You can split a string into an array (of strings) easily:
var words; // array of words
var text; // the input text string containing all the words
words = text.split("\n");
If you need help on how to you obtain the text from a file (or a http server) I'll edit my answer as you'll provide mode information in your question.
Using jQuery :
$('#somediv').load('some.txt', function(__textdata){
});

trouble using string.replace with regex

Given something a regex like this:
http://rubular.com/r/ai1LFT5jvK
I want to use string.replace to replace "subdir" with a string of my choosing.
Doing myStr.replace(/^.*\/\/.*\.net\/.*\/(.*)\/.*\z/,otherStr)
only returns the same string, as shown here: http://jsfiddle.net/nLmbV/
If you view the Rublar, it appears to capture what I want it to capture, but on the Fiddle, it doesn't replace it.
I'd like to know why this happens, and what I'm doing wrong. A correct regex or a correct implementation of the replace call would be nice, but most of all, I want to understand what I'm doing wrong so that I can avoid it in the future.
EDIT
I've updated the fiddle to change my regex from:
/^.*\/\/.*\.net\/.*\/(.*)\/.*\z/
to
/^.*\/\/.*\.net\/.*\/(.*)\/.*$/
And according to the fiddle, it just returns hello instead of https://xxxxxxxxxxx.cloudfront.net/dir/hello/Slide1_v2.PNG
It's that little \z in your regex.
You probably forgot to replace it with a $ sign. JavaScript uses ^ and $ as anchors, while Ruby uses \A and \z.
To answer your edit:
The match is always replaced as a whole. You'll want to group both the left side and the right side of the to-be-replaced part and reinsert it in the replacement:
url.replace(/^(.*\/\/.*\.net\/.*\/).*(\/.*)$/,"$1hello$2")
Before I get marked down, I know the question asks about regexp. The reason for this answer URLs are nearly impossible to process reliably with a regexp without writing fiendishly complex regexps. It can be done, but it makes your head hurt!
If you are doing this in a browser, you can use an A tag in your script to make things much simpler. The A tag knows how to parse them into pieces, and it lets you modify the pieces independently, so you only need to deal with the pathname:
//make a temporary a tag
var a = document.createElement('a');
//set the href property to the url you want to process
a.href = "scheme://host.domain/path/to/the/file?querystring"
//grab the path part of the url, and chop up into an array of directories
var dirs = a.pathname.split('/');
//set 2nd dir name - array is ['','path','to','file']
dirs[2]='hello';
//put the path back together
a.pathname = dirs.join('/');
a.href now contains the URL you want.
More lines, but also more hair left when you come back to change the code later.

How to find a Word that is enclosed from Html Tags?

I'm programming a spell checker in Javascript in combination with OpenOffice dictionary, and I have a serious problem.
I can find whole words using RegEx, but if the word looks like prog<b>ram</b>ing, I can find it if I remove all html tags with the .text() method from jQuery. But how can I replace this word and rebuild the original html structure?
Spellchecker.com does it very smartly - the spell check recognizes even words like prog<b>ram</b>ing if they are misspelled!
/([\s>"'])prog(<[^>]+>)ram(<[^>]+>)ing([\s\.,:;"'<])/g
will match your example
So roughly the following regex will find all instances of the word, even those broken with html
var regExp = new RegExp('([\s>"\'])' + word.split('').join('(<[^>]+>)') + '([\s\.,:;"\'<])',g);
God knows how that'll help you build a spellchecker though. I suspect the approach used in spellcheckers would be more like 'do a spellcheck assuming no html, and if there is html in a word then strip it out using something like the method below, and do a spellcheck as normal for the string you get:
String.prototype.stripHtml = function() {
return this.replace(/(<[^>]+>)/, '');
}
I would use something to pull out any HTML so that you are dealing with plaintext. I cannot speak for any tools like this in javascript but I'm sure they exists. If you can find something to 'scrub' the html out of your .text() you can run a search this way.
Try something like this: http://metacpan.org/pod/HTML::Scrubber

Categories