JavaScript Regex not after a specified character [duplicate] - javascript

This question already has answers here:
Negative lookbehind equivalent in JavaScript
(11 answers)
Closed 9 years ago.
How to match a pattern that is not after a specified character?
Let's say the specified character is a space and we want to match #match
Eg: #match - finds "#match", because no space before it
#match and #match - finds the first #match but not the second one because there is a space before it.
ANTHING#match - should also find "#match", because no space before it.

Have you tried something like this?
/[^ ](pattern)/
But that won't match patterns at the beginning of the string, so you could use that in combination with:
/^pattern/

I'm not sure... but for all trying to answer this question, here's a jsfiddle where you can just enter your REGEX where it says INSERT_REGEX_HERE and see if it works. Enjoy!
<div ng-app>
<form novalidate name="myForm">
<input name="input1" type="text" ng-model="input1" ng-pattern="/INSERT_REGEX_HERE/" />
<div ng-show="myForm.input1.$dirty && myForm.input1.$error.pattern">DOES NOT MATCH REGEX</div>
</form>
</div>
http://jsfiddle.net/MLhqT/2/

Related

How to escape ~ character in jQuery selector [duplicate]

This question already has answers here:
Need to escape a special character in a jQuery selector string
(7 answers)
Closed 4 years ago.
I'm trying to select an element this way
$('#QR~QID345')
because the element's id is QR~QID345 but the selector doesn't work with ~, is there any fix?
Changing the id is not an option in this case.
You can escape ~ with \\
alert( $('#QR\\~QID345').text() );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="QR~QID345">Hello</div>
You can use $.escapeSelector to escapes any character that has a special meaning in a CSS selector.
var id = $.escapeSelector("QR~QID345");
console.log($('#' + id).text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id="QR~QID345">
Hello World
</p>
As been noted by Rory McCrossan on the comment, $.escapeSelector was added on version 3.0
Doc: $.escapeSelector()

Javascript issue with Replace Character [duplicate]

This question already has answers here:
Stop cursor from jumping to end of input field in javascript replace
(3 answers)
Closed 5 years ago.
I am trying to replace special characters i.e <,>,& from input field on blur and keypress. And the code is working fine but the problem is, once user enter the desired string and then try to edit that string it will move the cursor to the end of the string, So the user isn't able to edit the string in between. How do I make this script more user friendly so that user can edit the existing string from anywhere.
HTML
<input type="text" class="prevent-special" name="name" value="HelloWorld" >
Script
$('.prevent-special').on('keypress blur',function(e){
//console.log(e.keyCode);
$(this).val($(this).val().replace(/\<|\>|\&+/g,''));
})
JS Example
Try this:
$('.prevent-special').on('keyup blur',function(e){
var start = this.selectionStart,
end = this.selectionEnd
$(this).val($(this).val().replace(/\<|\>|\&+/g,''))
this.setSelectionRange(start, end)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" class="prevent-special" name="name" value="HelloWorld" >

Regex to strip html tag with certain attribute

I have some invalidly-nested HTML like:
<form class="form1" method="get">
<div>
<input name="field1">
</form>
<form class="form2" method="get">
<input name="field1">
</form>
</div>
Yeah, it's a mess, don't ask. The invalid nesting is causing problems somewhere else. jQuery I think is expecting a closing </div>, and only finding it at the last one. It's then treating the second <form> tag as invalid, and also discarding the closing </form> immediately above it, and assuming everything between lines 1 and 9 are one form.
If I output these to the console:
$('.form1).html() - all of line 1 - 9
$('.form2).html() - undefined
So what I'm trying to do is treat the whole thing as a string, and use regex to strip out form2. I'm expecting a regex something like:
formText.replace(/(<form\b[^>]*>)[^<>]*(<\/form>)/gi, "");
but I'm not sure how to reference the specific form with class=form2.
There's also a problem with it being a multi-line string.
Update: added more detail, outlining why jQuery's remove() method isn't working. jQuery only thinks there's one form unfortunately.
Don't use regex to parse HTML. Since you're using jQuery, just use .remove():
$(function() {
$(".form2").remove();
});
JSFiddle
I ended up using:
formText = formText.replace(/(<form\b[^>]*form2+.*>[\s\S]+<\/form>)/gi, "");
The [\s\S] matches all characters including \n and \r to cover the newlines.
I could probably have made the part of the regex dealing with the class name more specific so I knew it was the class and not some other random form with a similar, but in practice it didn't matter (there was only one instance of the 2nd form, with a very specific class name).

Regex end in backslash

I'm using regex in javascript to validate a form. One of the form fields is a filepath so needs to end in a backslash.
Specifically, I'm using <input type="text" pattern="" /> and I want to fill out the Pattern attribute to validate it.
Now..
I understand you make a backslash literal by doubling up ie. \\
and I understand that you use the dollar ($) sign to find the end of the string.
So can anyone explain to me why $// and //$ don't work? And maybe give me an example of something that would work?
Thanks
I got it working if I match the entire input, like so .*\\$
Dropping the $ behaved ok too,
<form>
path: <input type="text" pattern=".*\\" title="ends in \">
</form>
(using Chrome 27)
You seem to be mixing up slash / with backslash \. A \\$ is different from a //$, and \\$ should work.

RegEx in javascript. Allow only letters, comma and punctuation

I try to make a RegEx for validating a form in javascript. The RegEx should only allow letters comma and punctuation. For instance I have this string:
Hi, this is a test of RegEx. Does it work
I've tried the following
/^[A-Za-z0-9,. ]{3,50}$/;
But it doesn't seems to work. Solutions?
Thanks!
EDIT:
This is my code:
<script type="text/javascript">
var RE_SSN = /^[A-Za-z0-9,. ]{3,50}$/;
function checkSsn(ssn){
if (RE_SSN.test(ssn)) {
alert("OK");
javascript:addAppointment(document.forms[0])
} else {
alert("NO!");
}
}
</script>
<div id="r">
<label for="receipt">Receipt</label><input type="checkbox" name="receipt" value="1"/>
</div>
<input type="button" value="Post it" onclick="checkSsn(this.form.content.value);"/>
You might need to escape the "." as that is a special character in regex.
/^[A-Za-z0-9,\. ]{3,50}$/;
Actually probably not. Try using http://www.regextester.com/ - I was able to get it to work anyway. Can you show us the full code for how you're implementing this?
if you want no punctuation at the beginning of the field:
/^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/
this also allows spaces (one scenario for this is last name - De La Hoya, or O'Doul)

Categories

Resources