replaceall

JavaScript replaceAll case-insensitive search using variable rather than a string

这一生的挚爱 提交于 2021-02-08 08:39:40
问题 I want to do a JavaScript replaceAll() using a variable (rather than a string) in a case-insensitive search, but also retaining the case of the matched text (in the return). For example, console.log('doc.p:', doc.p.toString().substring(0, 26)) var query = this.manager.store.get('q').value.toString(); console.log('query:', query, '| type:', typeof(query)) console.log(doc.p.toString().replaceAll(/(dna)/gi, '***$1***')) console.log(doc.p.toString().replaceAll(/(query)/gi, '***$1***')) is giving

get specific character

笑着哭i 提交于 2021-01-01 03:56:34
问题 I would like to remove Zero from a string, below is an example : String a : 020200218-0PSS-0010 a.replaceall("-(?:.(?!-))+$", "**REPLACEMENT**") Actual : 020200218-0PSS-0010 Expected : 020200218-0PSS-10 I'm using this regex to catch -0010 : -(?:.(?!-))+$ I just dont know what to do in the REPLACEMENT section to actually remove the unused zero (not the last zero for exemple "10" and not "1") Thanks for reading! 回答1: You could use something like: (?<=-)0+(?=[1-9]\d*$) It translates to "find all

get specific character

佐手、 提交于 2021-01-01 03:52:58
问题 I would like to remove Zero from a string, below is an example : String a : 020200218-0PSS-0010 a.replaceall("-(?:.(?!-))+$", "**REPLACEMENT**") Actual : 020200218-0PSS-0010 Expected : 020200218-0PSS-10 I'm using this regex to catch -0010 : -(?:.(?!-))+$ I just dont know what to do in the REPLACEMENT section to actually remove the unused zero (not the last zero for exemple "10" and not "1") Thanks for reading! 回答1: You could use something like: (?<=-)0+(?=[1-9]\d*$) It translates to "find all

get specific character

浪尽此生 提交于 2021-01-01 03:51:37
问题 I would like to remove Zero from a string, below is an example : String a : 020200218-0PSS-0010 a.replaceall("-(?:.(?!-))+$", "**REPLACEMENT**") Actual : 020200218-0PSS-0010 Expected : 020200218-0PSS-10 I'm using this regex to catch -0010 : -(?:.(?!-))+$ I just dont know what to do in the REPLACEMENT section to actually remove the unused zero (not the last zero for exemple "10" and not "1") Thanks for reading! 回答1: You could use something like: (?<=-)0+(?=[1-9]\d*$) It translates to "find all

java replaceAll not working for \n characters

早过忘川 提交于 2020-02-20 07:03:31
问题 I have a string like this: John \n Barber now I want to replace \n with actual new line character so it will become John Barber this is my code for this replaceAll("\\n", "\n"); but it is not working and giving me same string John \n Barber 回答1: You need to do: replaceAll("\\\\n", "\n"); The replaceAll method expects a regex in its first argument. When passing 2 \ in java string you actually pass one. The problem is that \ is an escape char also in regex so the regex for \n is actualy \\n so

How to ensure replaceAll will replace a whole word and not a subString

一个人想着一个人 提交于 2020-01-10 04:02:05
问题 I have an input of dictionary. The dictionary is iterated over to replace the key from dictionary in the text. But replaceAll function replaces the subString as well. How to ensure that it will match the whole word (as a whole and not as a subString ) String text= "Synthesis of 1-(2,6-dimethylbenzyl)-1H-indole-6-carboxylic acid [69-3] The titled compound (883 mg) sdvfshd[69-3]3456 as a white solid was prepared" dictionary= {[69-3]=1-(2,6-dimethylbenzyl)-1H-indole-6-carboxylic acid } for(Map

How to preserve newlines while reading a file using stream - java 8

做~自己de王妃 提交于 2020-01-02 01:20:11
问题 try (Stream<String> lines = Files.lines(targetFile)) { List<String> replacedContent = lines.map(line -> StringUtils.replaceEach(line,keys, values)) .parallel() .collect(Collectors.toList()); Files.write(targetFile, replacedContent); } I'm trying to replace multiple text patterns in each line of the file. But I'm observing that "\r\n"(byte equivalent 10 and 13) is being replaced with just "\r"(just 10) and my comparison tests are failing. I want to preserve the newlines as they are in the

Replace specific string by another - String#replaceAll()

独自空忆成欢 提交于 2019-12-31 04:32:08
问题 I'm actually developping a parser and I'm stuck on a method. I need to clean specifics words in some sentences, meaning replacing those by a whitespace or a null character. For now, I came up with this code: private void clean(String sentence) { try { FileInputStream fis = new FileInputStream( ConfigHandler.getDefault(DictionaryType.CLEANING).getDictionaryFile()); BufferedReader bis = new BufferedReader(new InputStreamReader(fis)); String read; List<String> wordList = new ArrayList<String>();

myString.replaceAll(regex,“”) not working as expected

六眼飞鱼酱① 提交于 2019-12-25 07:02:25
问题 As I've been asked here Deleting all regex instances starting with char '[' and ending with char ']' from a String. I want to delete all the regex 's between any opening [ and closing ] in a String . The answer i used is myString = myString .replaceAll("\\[([\\w\\%\\@]+)\\]",""); . provided by @cuong hoang. Now the method is working well with most of the Srtring 's but It's making some problems with this String : "[Intro:]\r\n" + "[Tuning Tv]\r\n" + "[Eminem Snortin Crack]\r\n" + "\r\n" + "