Aurel Engle Engelmann Aurel Engle Engelmann - 1 month ago 6
Javascript Question

different regex expressions but actually forbid newline

I am going nuts on this for a few days now. I have read and tried many previously answered questions close to this topic, I've fiddled with all kinds of expressions on (very awesome page BTW) but I can't for the life of me figure it out.

I want to do the following in JavaScript/Jquery (this has to do with contenteditable="true" elements where I want to restrict user input).
Some elements are for text input without spaces and allowed numbers (abc1_d-ef), some for free text (abc. d2ef, gh6i? j8kl: mno!), some for integers (123), some for decimals (1,2 / 1.2).
BUT I want to always forbid a newline character or tab (\n \r \t \f).


var pattern = new RegExp(.........);
var text = $("#my-id").html();

var test = pattern.test(text);
// test should be true for correct text / integer / decimal
// but should be false as soon as text contains a newline, tab etc.

So basically I'm looking for 4 different expressions:

  • letters, numbers, underscore, hyphen

  • letters, numbers, spaces, special characters and such

  • numbers 0-9

  • numbers, comma and dot

  • but none of them with newline etc.

I hope I could make myself clear.

Jan Jan

For all your requirements, you can use:

^[-,.\w ]+$

See a demo on