1

currently I am very confused! I have been trying to solve this for a while, and can't seem to crack it. I can't even find an answer on Google.

Currently I am using this Regex I wrote in Javascript:

((?: {4,}|\t+)(?:.*))

Simply, it matches everything that is 4+ spaces out or 1+ tabs out in a textarea. Yea, that sounds familiar you say? It is simply what Stack Overflow does for codeblocks. Now, I have run in to an issue.

Basically I am wanting to replace all of these instances found in the textarea, but first I want to back them up to an array. Now, I am not sure how I grab each match found, and insert it in to the array.

Here is what I have so far:

.replace(/((?: {4,}|\t+)(?:.*))/gi, function (m, g1, g2) {
    return //STUCK HERE
}); //Matches Bold

So simply, I just want to grab all matches found in a textarea (Either indented or 4 spaces) and then I would like to add the contents of the match to an array. How would I go about doing this? Please Help!

2
  • What exactly is the point of (?:.*)? Isn't just .* exactly the same? Also, we can't help you with replacing that text if you don't tell us what you want to replace it with. Commented Aug 22, 2015 at 16:06
  • Hello @Siguza, first off, the point of the (?:.*) is so it does not match that in particular, I want it to just match the whole regex as a whole. Second, simply I am trying to backup codeblocks because I am making a Markdown parser. Then after I run all of the other regexs I would like to restore the code blocks. Now, for what I would like it to return, it could return just the same thing I guess. Commented Aug 22, 2015 at 16:08

2 Answers 2

1

You can hit two targets with one bullet:

var items = [];
var re = /((?: {4,}|\t+)(?:.*))/g;
textarea.value = textarea.value.replace(re, function ($0, $1) {
    items.push($1);
    return 'replacement';
});

If you want to get the code blocks back then:

var codeLines = [];
var reMarkers = /\{\{(.*?)\}\}/g;
var reCodeBlocks = /((?: {4,}|\t+)(?:.*))/g;
var text = textarea.value;

// save and remove code blocks
text = text.replace(reCodeBlocks, function ($0, $1) {
    var marker = '{{' + codeLines.length + '}}';
    codeLines.push($1);
    return marker;
});

// revert to previous state
text = text.replace(reMarkers, function ($0, $1) {
    return codeLines[parseInt($1, 10)];
});
Sign up to request clarification or add additional context in comments.

6 Comments

Hello, thank you for your answer. Now, eventually, how would I go and replace all of the replacements with the items from the array?
@MichaelJones I would change the regexp slightly in order to back up every lines of the textarea rather than just the idented ones. Otherwise it can be hard to manage... Yeah, you need the line numbers at least.
@MichaelJones The easiest way to do this would be to split the content based on the line breaks, like this: var lines = textarea.value.split(/\n/g). Though, I wonder why you are looking for the indented lines only?
Hmm, what if i just kept the number of each replacement then I returned for instance replacement1 and replacement2. Would that be possible? Then from the array you could grab index #1 and #2. Does that make sense? And is that possible?
Ok simply, I would not like to allow styles in code blocks. So it would be easier to just back up the code blocks, then after I style everything, replace the styles code blocks with the old. That way the code blocks would NOT contain any formatted text.
|
1

If you want to get the array of matches, you can use match() function :

var matchesArray = $('textarea').val().match('/((?: {4,}|\t+)(?:.*))/gi');

and if you want to replace, use simple replace() function :

$('textarea').val().replace('/((?: {4,}|\t+)(?:.*))/gi', 'replaceWith');

Hope this helps.

1 Comment

I just meant an array :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.