0

I'm trying to test user input that doesn't begin with or end in spaces, note that I am not trying to return the match.

I am aiming for a JavaScript-less solution so that the website can be used on web browsers like “Brave,” or used by users that activate a JavaScript blocker like “NoScript.”

It is impossible for my client to input a tab, newline, or other space like character.

The input is on 1 line, multiline is irrelevant.

I am allowing spaces within the input, just not at the beginning or end.

Examples of what should work:

“lorem ipsum”

“lorem ipsum dolor”

Examples of what should fail:

“ lorem ipsum ”

“abc    ”

I'm pretty sure I'll need a negative lookbehind and lookahead, but I still haven't found a solution.

Some failed Regular Expressions I've come up with so far, while using regexr.com:

^(?<=[^ ]+).+(?=[^ ]+)$

^(?<! +)[ \w]+(?! +)$

^(?<! +)[^ ]+(?! +)$

^ {0}.+ {0}$

^([^ ]*).+([^ ]*)$

^\S*$
Community
  • 1
  • 1
  • Could you just [trim](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim) it instead of detecting it? – ray Mar 16 '20 at 05:13
  • This should answer your requirement https://stackoverflow.com/questions/34974942/regex-for-no-whitespace-at-the-beginning-and-end – Kumar Ashutosh Mar 16 '20 at 05:19
  • This should answer your question https://stackoverflow.com/questions/34974942/regex-for-no-whitespace-at-the-beginning-and-end – Kumar Ashutosh Mar 16 '20 at 05:21

4 Answers4

0

Why don't you use trim() in the beginning and end of the input value?

Source : trim()

Manjuboyz
  • 6,978
  • 3
  • 21
  • 43
  • Sorry, I forgot to mention that this is being used on a page that doesn't allow JavaScript, so JQuery isn't an option. I'll update the question to reflect this. –  Mar 16 '20 at 05:16
  • Ok, what platform you want this to work if not from JS or JQ? – Manjuboyz Mar 16 '20 at 05:18
  • It's inside an HTML form. –  Mar 16 '20 at 05:22
0

const exp2 = /^\w+.*\w+$/
console.log(exp2.test("lorem ipsum")); // true
console.log(exp2.test("lorem ipsum dolor")); // true
console.log(exp2.test(" lorem ipsum ")); // false
console.log(exp2.test("abc    ")); // false

// or the opposite:

// (?: ) - non-capturing group
// ^ - start of string
// \s+ - one or more whitespace characters
// .+ - any sequence of characters
// | - OR
// .+ - any sequence of characters
// \s+ - one or more whitespace characters
// $ - end of string
const exp = /(?:^\s+.+|.+\s+$)/;

console.log(exp.test("lorem ipsum")); // false
console.log(exp.test("lorem ipsum dolor")); // false
console.log(exp.test(" lorem ipsum ")); // true
console.log(exp.test("abc    ")); // true
ray
  • 26,557
  • 5
  • 28
  • 27
  • Sorry, I cannot accept this. This doesn't solve my problem at all. Actually... this is exactly the opposite, can it be made to negate what it accepts right now? –  Mar 16 '20 at 05:25
  • Updated with even simpler negated expression. – ray Mar 16 '20 at 05:32
  • Very short, almost what I need, but enforces a minimum of 2 characters. –  Mar 16 '20 at 05:37
0

This pattern may be solve your problem:

^[^ ](.*?)[^ ]$

<form>
<input pattern="^[^ ](.*?)[^ ]$" required>
</form>
Ritesh Khandekar
  • 3,885
  • 3
  • 15
  • 30
  • 1
    I overlooked the lazy quantifier, this is the simplest solution I've encountered yet. –  Mar 16 '20 at 05:31
  • Actually, I've gotten it work with /^[^ ].+[^ ]$/ now, is there any difference between them? –  Mar 16 '20 at 05:35
  • Your new updated version doesn't allow spaces in between words. –  Mar 16 '20 at 05:40
  • The original `/^[^ ](.*?)[^ ]$/` is okay, your answer is equal to ray hatfield's answer. If my client is inputting only 1 character, chances are that they aren't actually trying to do what the form is requesting from them. –  Mar 16 '20 at 05:44
  • Okay. I got it. You were right to use `/^[^ ].+[^ ]$/` Regex. – Ritesh Khandekar Mar 16 '20 at 05:46
  • The smallest I've gotten is `^\S+$`, but this doesn't allow spaces in between. –  Mar 16 '20 at 05:52
  • `^\S(.*?)\S$` this may be shorter – Ritesh Khandekar Mar 16 '20 at 06:02
0

You may try this too (uppercase S).

[code] ^\S(.*\S)?$ [/code]

Slkrasnodar
  • 824
  • 6
  • 10
  • Enforces 2 character minimum. If anyone gets a 1 character minimum, I'll accept that instead, but at this point, we should flag my question as a duplicate. –  Mar 16 '20 at 05:48