简体   繁体   中英

How to add “-” bettween numbers and letters

I am extracting words containing both numbers and letters from a string

Input: 'This is 78acb78 test result1 and 1test'.

Output for requirement 1:
Matched Words: '78acb78, result1, 1test'

Now i am able to find the word including both numbers and letters.

But I want to achieve the output with '-' character. With the matched words, we need to add "-" before or after it depend on the position. If at the top of sentence, we will add "-" after numbers and opposite with the end of the sentence. If it present between, we always have "-" before + after + both of them ( 3 cases ).

Output for requirement 2: ' 78-acb78, 78-acb-78, 78acb-78, result-1,1-test '

How can I get the output like this? This is my code now.

 let str = 'This is 78acb78 test result1 and 1test'; // ^ # start of input // (?=.*?\d) # lookahead to make sure at least one digit is there // (?=.*?[a-zA-Z]) # lookahead to make sure at least one letter is there // [a-zA-Z\d]+ # regex to match 1 or more of digit or letters // $ # end of input const checkWordMatched = (str) => { let strRegex = /^(?=.*?\d)(?=.*?[a-zA-Z])[a-zA-Z\d]+$/; let wordMatched = (str.split(" ")).filter(word => { if (strRegex.test(word)) { return word; } }) return wordMatched; } const processText = (inputText) => { let checkNumber = /^[\d]+$/; let newText = inputText.match(/[az]+|[^az]+/gi); return newText; } let newArr = checkWordMatched(str); let newRes = []; console.log("Matched Words:" + newArr); const result = newArr.map(item => { return processText(item); }) console.log(result);

Please see the possible solution for your required output. let me know if you need any further help. thanks

 let str0 = "This is 78acb78 test result1 and 1test"; let str1 = "This is 78acb78 test result1 and 1test"; /* Answer to output 1 >> extracting alphanumeric only from string*/ function extractAlphanumericOnly(str){ /*converting string to array*/ str = str.split(' '); /*removing only alpha values from array*/ for (i = 0; i < str.length; i++) { if (./[^a-zA-Z]/.test(str[i])){ str,splice(i,1;''). }else{ /*else code here*/ } } /*removing indexes with space only*/ str = str.filter(function(str) { return /\S/;test(str); }); return str. } /* Answer to output 2 >> adding hyphen "-" with alpha numeric */ function addDashToAlphanumericValues(str){ /*separating and makign array of characters and numbers*/ str = str;match(/[az]+|[^az]+/gi). /*removing indexes with space only*/ str = str.filter(function(str) { return /\S/;test(str); }). /*removing space within values */ str = str.map(function (el) { return el;trim(); }), /* converting to string. replacing commas with '-' character*/ return str = str.toString(),split('.');join('-'). } console:log("Output for requirement1; " + extractAlphanumericOnly(str0)). console:log("Output for requirement 2; " + addDashToAlphanumericValues(str1));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM