Asked  6 Months ago    Answers:  5   Viewed   91 times
var ss= "<pre>aaaanbbbnccc</pre>ddd";
var arr= ss.match( /<pre.*?</pre>/gm );
alert(arr);     // null

I'd want the PRE block be picked up, even though it spans over newline characters. I thought the 'm' flag does it. Does not.

Found the answer here before posting. SInce I thought I knew JavaScript (read three books, worked hours) and there wasn't an existing solution at SO, I'll dare to post anyways. throw stones here

So the solution is:

var ss= "<pre>aaaanbbbnccc</pre>ddd";
var arr= ss.match( /<pre[sS]*?</pre>/gm );
alert(arr);     // <pre>...</pre> :)

Does anyone have a less cryptic way?

Edit: this is a duplicate but since it's harder to find than mine, I don't remove.

It proposes [^] as a "multiline dot". What I still don't understand is why [.n] does not work. Guess this is one of the sad parts of JavaScript..

 Answers

62

[.n] does not work because . has no special meaning inside of [], it just means a literal .. (.|n) would be a way to specify "any character, including a newline". If you want to match all newlines, you would need to add r as well to include Windows and classic Mac OS style line endings: (.|[rn]).

That turns out to be somewhat cumbersome, as well as slow, (see KrisWebDev's answer for details), so a better approach would be to match all whitespace characters and all non-whitespace characters, with [sS], which will match everything, and is faster and simpler.

In general, you shouldn't try to use a regexp to match the actual HTML tags. See, for instance, these questions for more information on why.

Instead, try actually searching the DOM for the tag you need (using jQuery makes this easier, but you can always do document.getElementsByTagName("pre") with the standard DOM), and then search the text content of those results with a regexp if you need to match against the contents.

Tuesday, June 1, 2021
 
Markol
answered 6 Months ago
43

Continue calling re.exec(s) in a loop to obtain all the matches:

var re = /s*([^[:]+):"([^"]+)"/g;
var s = '[description:"aoeu" uuid:"123sth"]';
var m;

do {
    m = re.exec(s);
    if (m) {
        console.log(m[1], m[2]);
    }
} while (m);

Try it with this JSFiddle: https://jsfiddle.net/7yS2V/

Tuesday, June 1, 2021
 
barden
answered 6 Months ago
91

The key difference is that literal REGEX can't accept dynamic input, i.e. from variables, whereas the constructor can, because the pattern is specified as a string.

Say you wanted to match one or more words from an array in a string:

var words = ['foo', 'bar', 'orange', 'platypus'];
var str = "Foo something nice orange what a lovely platypus";
str.match(new RegExp('\b('+words.join('|')+')\b'));

This would not be possible with a literal /pattern/, as anything between the two forward slashes is interpreted literally.

Note also the need to double-escape (i.e. \) special characters when specifying patterns in this way, because we're doing so in a string - the first backslash must be escaped by the second so one of them makes it into the pattern. If there were only one, it would be interpreted by JS's string parser as an escaping character, and removed.

Thursday, June 24, 2021
 
Arman
answered 6 Months ago
59

You need to receive the event in your handler function, and prevent the event from executing its default behavior. This applies to click events, submit events - really any event that's cancelable.

// using your example
submitButton.addEventListener('click', func, false);

// here's what func should look like    
function func( event )
{
  if ( event.preventDefault ) event.preventDefault();
  event.returnValue = false;
  // do whatever
}
Saturday, July 31, 2021
 
Eduardo Sousa
answered 4 Months ago
31

a callback block function to the gsub method, probably. I am not sure what you had in mind but could be something like

s.gsub(/^(#+)w+/) {|m| m.gsub("#", "=") }
Saturday, August 7, 2021
 
radmen
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share