I want to match string between two string with Regex including newlines.
For example, I have the next string:
{count, plural,
one {apple}
other {apples}
}
I need to get string between plural, and one. It will be \n*space**space*.
I tried this Regex:
/(?:plural,)(.*?)(?:one)/gs
It works, but not in JS. How to do that with JavaScript?
To match the everything including newline character, you can use [\s\S] or [^]
var str = `{count, plural,
one {apple}
other {apples}
} `;
console.log(str.match(/(?:plural,)([\s\S]*?)(?:one)/g));
console.log(str.match(/(?:plural,)([^]*?)(?:one)/g));
It doesn´t work because your testing with the wrong regex engine.
`/s` does not exist in the JS regex engine, only in pcre
It must be something like:
/(?:plural,)((.|\n)*?)(?:one)/g
Hope it helps.
Related
I have this regex /[\W_]+/g that I use to remove any non-alphanumeric characters. However it does not remove brackets.
What I need is for it to remove any kind of bracket/paranthesis so that a string like Hello (world) becomes helloworld.
A string like Hello(world) becomes helloworld, but it does not work if there is a space between them.
Is this possible?
You should be able to use this Java / JavaScript compliant regex according to RegexBuddy 4.x:
([\W\s_]+)
And just replace anything it matches with '' or ""
Following the documentation here, something like this:
#set($mystring = "Hello (world)! It's _{now}_ or -- [never]...;")
$mystring.replaceAll("</?([\W\s_]+)/?>", "");
=>
HelloworldItsnowornever
I'm trying to find characters when preceded with exact string but not the preceding string. how to do that?
I have the string
+1545464454<+440545545454<+210544455454<+75455454545
the above string are phone numbers with international prefix, but some of them have 0 between prefix and number and I need to take it out.
I have /(\+4|\+44|\+7 ... allprefixes here...)0/g but this selects the prefix as well and I need to select only 0
I's in the javascript
You're almost close to that. Just use Capturing groups and replace function like below. Most languages support capturing groups.
/(\+(?:4|44|7 ... allprefixes here without `+`...))0/g
REplacement string:
$1
or \1
If you're on PHP, \K should work. \K discards previously matched characters from printing at the final.
'~\+(?:4|44|7)\K0~g'
In javascript.
> var str = "+1545464454<+440545545454<+210544455454<+75455454545"
> str.replace(/(\+(?:44|21|7|4))0/g, "$1")
'+1545464454<+44545545454<+21544455454<+75455454545'
If your language supports lookbehinds, you can use it as used in the following regex
/(?<=\+(4|44|7))0/g
Javascript doesn't support it. So you'll need to use something like this
str.replace(/(\+(4|44|7))0/, "$1");
Tried to search for /\,$/ online, but coudnt find anything.
I have:
coords = coords.replace(/\,$/, "");
Im guessing it returns coords string index number. What I have to search online for this, so I can learn more?
/\,$/ finds the comma character (,) at the end of a string (denoted by the $) and replaces it with empty (""). You sometimes see this in regex code aiming to clean up excerpts of text.
It's a regular expression to remove a trailing comma.
That thing is a Regular Expression, also known as regex or regexp. It is a way to "match" strings using some rules. If you want to learn how to use it in JavaScript, read the Mozilla Developer Network page about RegExp.
By the way, regular expressions are also available on most languages and in some tools. It is a very useful thing to learn.
That's a regular expression that finds a comma at the end of a string. That code removes the comma.
// defines a JavaScript regular expression, used to match a pattern within a string.
\,$ is the pattern
In this case \, translates to ,. A backslash is used to escape special characters, but in this case, it's not necessary. An example where it would be necessary would be to remove trailing periods. If you tried to do that with /.$/ the period here has a different meaning; it is used as a wildcard to match [almost] any character (aside for some newlines). So in this case to match on "." (period character) you would have to escape the wildcard (/\.$/).
When $ is placed at the end of the pattern, it means only look at the end of the string. This means that you can't mistakingly find a comma anywhere in the middle of the string (e.g., not after help in help, me,), only at the end (trailing). It also speeds of the regular expression search considerably. If you wanted to match on characters only at the beginning of the string, you would start off the pattern with a carat (^), for instance /^,/ would find a comma at the start of a string if one existed.
It's also important to note that you're only removing one comma, whereas if you use the plus (+) after the comma, you'd be replacing one or more: /,+$/.
Without the +; trailing commas,, becomes trailing commas,
With the +; no trailing comma,, becomes no trailing comma
I am looking for a regex that will give me the index of the last space in a string using javascript.
I was using goolge to find a suitable regex, but no success.
Even the SO-Question Regex to match last space character does not hold a solution because the goal there was to remove more than one character in the end.
What is the correct regex?
As I commented I would just use lastIndexOf() but here is a regex solution:
The regex / [^ ]*$/ finds the last space character in a string. Use it like this:
// Alerts 9
alert("this is a str".search(/ [^ ]*$/));
The correct solution is not using a regex at all but the built-in lastIndexOf method strings have. Regexes are meant to match strings, not give you indexes (even though grouped matchs may be returned as index+length instead of a string - C-based regex libraries usually do so to avoid unnecessary copying)
I have a string and I want to validate that string so that it must not contain certain characters like '/' '\' '&' ';' etc... How can I validate all that at once?
You can solve this with regular expressions!
mystring = "hello"
yourstring = "bad & string"
validRegEx = /^[^\\\/&]*$/
alert(mystring.match(validRegEx))
alert(yourstring.match(validRegEx))
matching against the regex returns the string if it is ok, or null if its invalid!
Explanation:
JavaScript RegEx Literals are delimited like strings, but with slashes (/'s) instead of quotes ("'s).
The first and last characters of the validRegEx cause it to match against the whole string, instead of just part, the carat anchors it to the beginning, and the dollar sign to the end.
The part between the brackets ([ and ]) are a character class, which matches any character so long as it's in the class. The first character inside that, a carat, means that the class is negated, to match the characters not mentioned in the character class. If it had been omited, the class would match the characters it specifies.
The next two sequences, \\ and \/ are backslash escaped because the backslash by itself would be an escape sequence for something else, and the forward slash would confuse the parser into thinking that it had reached the end of the regex, (exactly similar to escaping quotes in strings).
The ampersand (&) has no special meaning and is unescaped.
The remaining character, the kleene star, (*) means that whatever preceeded it should be matched zero or more times, so that the character class will eat as many characters that are not forward or backward slashes or ampersands, including none if it cant find any. If you wanted to make sure the matched string was non-empty, you can replace it with a plus (+).
I would use regular expressions.
See this guide from Mozillla.org. This article does also give a good introduction to regular expressions in JavaScript.
Here is a good article on Javascript validation. Remember you will need to validate on the server side too. Javascript validation can easily be circumvented, so it should never be used for security reasons such as preventing SQL Injection or XSS attacks.
You could learn regular expressions, or (probably simpler if you only check for one character at a time) you could have a list of characters and then some kind of sanitize function to remove each one from the string.
var myString = "An /invalid &string;";
var charList = ['/', '\\', '&', ';']; // etc...
function sanitize(input, list) {
for (char in list) {
input = input.replace(char, '');
}
return input
}
So then:
sanitize(myString, charList) // returns "An invalid string"
You can use the test method, with regular expressions:
function validString(input){
return !(/[\\/&;]/.test(input));
}
validString('test;') //false
You can use regex. For example if your string matches:
[\\/&;]+
then it is not valid. Look at:
http://www.regular-expressions.info/javascriptexample.html
You could probably use a regular expression.
As the others have answered you can solve this with regexp but remember to also check the value server-side. There is no guarantee that the user has JavaScript activated. Never trust user input!