regex

How to disallow characters (and replace them) on type or paste with jQuery?

℡╲_俬逩灬. 提交于 2021-02-05 07:12:09
问题 I’m trying to find a way to replace disallowed characters from being entered or pasted into all input fields (textboxes and textareas essentially) in a form. Any time a user pastes text that contains one or more disallowed characters I would want the character replaced with an empty string ‘’ but have the rest of the text intact. If they type and type the character I would just want “nothing” to happen (e.g. the character not to appear if they type it). Is there a code sample that exists on

Regex pattern matching literal repeated \n

爱⌒轻易说出口 提交于 2021-02-05 07:11:25
问题 Given a literal string such as: Hello\n\n\n\n\n\n\n\n\n\n\n\nWorld I would like to reduce the repeated \n 's to a single \n . I'm using PHP, and been playing around with a bunch of different regex patterns. So here's a simple example of the code: $testRegex = '/(\\n){2,}/'; $test = 'Hello\n\n\n\n\n\n\n\n\nWorld'; $test2 = preg_replace($testRegex ,'\n',$test); echo "<hr/>test regex<hr/>".$test2; I'm new to PHP, not that new to regex, but it seems '\n' conforms to special rules. I'm still

Regular expression quoting in Python

落爺英雄遲暮 提交于 2021-02-05 07:11:05
问题 How should I declare a regular expression? mergedData = re.sub(r'\$(.*?)\$', readFile, allData) I'm kind of wondering why this worked. I thought that I need to use the r'' to pass a regular expression. mergedData = re.sub("\$(.*?)\$", readFile, allData) What does "\$" result in in this case? Why? I would have thought "$" . 回答1: I thought that I need to user the r'' to pass a regular expression. r before a string literal indicates raw string, which means the usual escape sequences such as \n

PHP Regex expression excluding <pre> tag

拥有回忆 提交于 2021-02-05 07:10:45
问题 I am using a WordPress plugin named Acronyms (https://wordpress.org/plugins/acronyms/). This plugin replaces acronyms with their description. It uses a PHP PREG_REPLACE function. The issue is that it replaces the acronyms contained in a <pre> tag, which I use to present a source code. Could you modify this expression so that it won't replace acronyms contained inside <pre> tags (not only directly, but in any moment)? Is it possible? The PHP code is: $text = preg_replace( "|(?!<[^<>]*?)(?<![?.

Regex to prevent trailing spaces and extra spaces

不问归期 提交于 2021-02-05 07:10:27
问题 Right now I have a regex that prevents the user from typing any special characters. The only allowed characters are A through Z, 0 through 9 or spaces. I want to improve this regex to prevent the following: No leading/training spaces - If the user types one or more spaces before or after the entry, do not allow. No double-spaces - If the user types the space key more than once, do not allow. The Regex I have right now to prevent special characters is as follows and appears to work just fine,

Regex: must start with a letter or a number but the rest can be anything

谁说我不能喝 提交于 2021-02-05 07:01:07
问题 I am trying to construct a pattern in order to use in validation. My goal is to have the first character to be a letter or a number, the rest anyhing. i.ex: A'r4nd0m! 9!h3ll0. b1llin6s I thought of: [a-zA-Z0-9_/][.*]++ What would be the solution? Thank you! 回答1: As I’ve commented, a letter or a number is [\pL\pN] . Therefore a string beginning with one of those would match the pattern /^[\pL\pN]/ 回答2: If the first number is a number or letter, you have ^[A-Za-z0-9] . (The ^ matches the

How can I add a comma after each 3 digits?

笑着哭i 提交于 2021-02-05 06:57:07
问题 This is simplified of my code: $("#annual_sales").on('keyup', function () { $(this).val( $(this).val().replace(/(\d{3})/g, "$1,") ); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="annual_sales" type="text" /> I'm trying to add a comma after every 3 digits. The patterns works well here , but as you can see (in the code snippet above) it doesn't work in the JS. Any idea what's wrong? 回答1: In your current pattern (\d{3}) you add a comma

Difference between two regex: “([^”]*)“ vs ”(.*?)"

為{幸葍}努か 提交于 2021-02-05 06:52:54
问题 I am learning about using cucumber's step defintion, which use regex. I came across the following different usages and would like to know if there's some material difference between the two approaches of capturing a group within a pair of double quotes: approach one: "([^"]*)" approach two: "(.*?)" For example, consider a string input: 'the output should be "pass!"' . Both approaches would capture pass! . Are there inputs where two the approaches capture differently; or are they equivalent?

Regular Expression for Same Consecutive Numbers

喜欢而已 提交于 2021-02-05 06:50:23
问题 How do i write a regex to handle all instances where a the substring is 6 or more consecutive numbers such as this? 000000 111111 222222 333333 444444 555555 I tried [0-9]{6,} . I plan to negative this afterwards so I can nullify strings with these cases. Thanks in advance! 回答1: To match strings only consisting of 6 or more identical digits, you may use ^([0-9])\1{5,}$ The pattern matches: ^ - start of string ([0-9]) - Capturing group 1 matching a digit \1{5,} - 5 or more occurrences (due to

Java / Replace all Quotation mark

∥☆過路亽.° 提交于 2021-02-05 06:50:18
问题 I to replace all Quotation mark in sentence to some word. Input: hello (and "How" are you"?") Output: hello (and WORD are youWORD) I try this is Java: mySentence.replaceAll("[\"].*[\"]", "WORD"); But nothing happens. How I can resolve it? 回答1: You can use: String replaced = mySentence.replaceAll("\".*?\"", "WORD"); Or better: String replaced = mySentence.replaceAll("\"[^\"]*\"", "WORD"); //=> hello (and WORD are youWORD) 回答2: you could easilly do this by converting a string in a character