Jump to content

Recommended Posts

Im not superbly versed with javascript. So I come and ask.

 

I have a function:

			if(defaults.nospace)
		{
			var pattern = new RegExp('[ ]+', 'g');

			val = val.replace(pattern, '');
		}

 

Well thats an if-else from it, but the one in question. the function itself is something that filters text inputs forces them to be letters only, letters and numbers, numbers only, uppercase, lowercase, etc.. issue is, is the above snipplet, you can set it to true or false. and if set to true its supposed to remove the spaces if its set to false its supposed to leave the spaces. Question is, that doesn't to me look as though its looking for one or the other, it seems like its only looking for if its set.. can anyone help me out, tell me how i can make that find the true|false?

Link to comment
https://forums.phpfreaks.com/topic/182589-finding-truefalse/
Share on other sites

It is "finding" true/false.  The if expects a condition that evaluates to either true or false. In this case the condition is simply the value of defaults.nospace. So, if that is false then what is inside the if block will not be executed (i.e., spaces will not be removed from val). If it is true then the spaces in val will be removed.

 

If you wanted to be super-specific, you could change the if line to read if (defaults.nospace == true) but that wouldn't change behaviour for true/false values.

 

[ot]

Since you're not superbly versed with JavaScript, I don't know whether you know about the shorthand form of writing regular expressions in JS.  Instead of using new RegExp, you can use the form /regex/modifiers where regex is your regular expression ([ ]+) and modifiers are your pattern modifiers (g).  To put this into context, your own code could be written in the two following ways:

 

if (defaults.nospace)
{
    var pattern = /[ ]+/g;
    val = val.replace(pattern, '');
}

 

if (defaults.nospace)
{
    val = val.replace(/[ ]+/g, '');
}

 

Also note that since your character class ([…]) only contains one value (the space) it is not strictly necessary. The regexp might as well be / +/g which would work in exactly the same manner.

[/ot]

Link to comment
https://forums.phpfreaks.com/topic/182589-finding-truefalse/#findComment-963862
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.